---
title: "10x faster python test iteration via fork(2)"
description: "Benchling cut Python test iteration from 10 seconds to about 1 second by preloading slow-to-import dependencies and SQLAlchemy models in a parent process, then fork()ing a child that imports the changed app and test code, in the style of Rails' zeus. importlib.reload() fails because reverse dependencies keep stale bindings."
url: "https://swe.quest/articles/10x-faster-python-test-iteration-via-fork-2-b4ce9e741110afab"
canonical: "https://benchling.engineering/10x-faster-python-test-iteration-via-fork-2-3aae52d2f6"
source: "Benchling Engineering"
published: "2023-07-20"
difficulty: "medium"
kind: "Case study"
reading_minutes: 8
tags: ["python","developer-productivity","testing","memory-management","linux"]
---

# 10x faster python test iteration via fork(2)

A post on [Benchling Engineering](https://swe.quest/sources/benchling.md), published 2023-07-20, 8 min read, Case study. Original: [https://benchling.engineering/10x-faster-python-test-iteration-via-fork-2-3aae52d2f6](https://benchling.engineering/10x-faster-python-test-iteration-via-fork-2-3aae52d2f6)

## Summary

Benchling cut Python test iteration from 10 seconds to about 1 second by preloading slow-to-import dependencies and SQLAlchemy models in a parent process, then fork()ing a child that imports the changed app and test code, in the style of Rails' zeus. importlib.reload() fails because reverse dependencies keep stale bindings. Modules are split into tiers by dependency position and edit frequency, so a file change discards as little import work as possible. Calling gc.freeze() before fork avoids copy-on-write page copies caused by refcount and GC header writes, saving 160 MiB of unique set size, while calling gc.collect() first is counterproductive because freed holes get refilled in copied pages.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Python](https://swe.quest/tags/python.md) (Languages)
- [Developer productivity](https://swe.quest/tags/developer-productivity.md) (Engineering practices)
- [Testing](https://swe.quest/tags/testing.md) (Engineering practices)
- [Memory management](https://swe.quest/tags/memory-management.md) (Compilers & runtimes)
- [Linux](https://swe.quest/tags/linux.md) (Infrastructure)

## Prerequisites

- Python import system ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- Unix fork and processes
- Garbage collection basics ([easy posts on Memory management](https://swe.quest/tags/memory-management.md?difficulty=easy))

## Keywords

fork(2), importlib.reload, gc.freeze, zeus, SQLAlchemy, copy-on-write, CPython

## Related posts

- [How posix\_spawn & close\_range “fixed” cold starts](https://swe.quest/articles/how-posix-spawn-close-range-fixed-cold-starts-0c769ed6733a0a1b.md): Neon · 2024-02-21 · Medium · 14 min read · Linux, Performance engineering, Debugging, Rust, PostgreSQL
  Neon's Pageserver regressed cold-start and first-query-after-idle latency to seconds after it began shutting down idle per-tenant walredo sidecar processes.
- [Introducing Immortal Objects for Python](https://swe.quest/articles/introducing-immortal-objects-for-python-dcfddb697b3a74fe.md): Meta Engineering · 2023-08-15 · Medium · 4 min read · Python, Memory management, Runtimes & VMs, Concurrency
  Instagram's pre-fork Django servers were losing shared memory to copy-on-write because CPython mutates reference counts and GC headers even on effectively read-only objects.
- [Lazy is the new fast: How Lazy Imports and Cinder accelerate machine learning at Meta](https://swe.quest/articles/lazy-is-the-new-fast-how-lazy-imports-and-cinder-accelerate-machine-learning-at-1dca895716af1470.md): Meta Engineering · 2024-01-18 · Easy · 5 min read · Python, Runtimes & VMs, Performance engineering, Machine learning, Developer productivity
  Meta rolled out the Cinder Python runtime with Lazy Imports, which transparently defers all imports until first use, across ML frameworks and Jupyter kernels.
- [Meta contributes new features to Python 3.12](https://swe.quest/articles/meta-contributes-new-features-to-python-3-12-9b216e044692b29d.md): Meta Engineering · 2023-10-05 · Medium · 6 min read · Python, Runtimes & VMs, Performance engineering, Compilers, Open source
  Meta upstreamed several features into CPython 3.12, many derived from its Cinder runtime: immortal objects (PEP 683) that skip reference counting to cut copy-on-write memory in forked Instagram workers, the @typing.override decorator (PEP 698), and inlined comprehensions (PEP 709) running up to 2x faster.
- [Zero-Downtime Patching Part 1: Prewarming](https://swe.quest/articles/zero-downtime-patching-part-1-prewarming-370c0c7ccc8bd249.md): Neon · 2026-03-27 · Medium · 4 min read · PostgreSQL, Deployment, Performance engineering, Caching
  Neon and Databricks Lakebase avoid the post-restart performance hit of cold Postgres caches by prewarming a replacement compute before planned maintenance.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
