What this thread does
Do first: construct workers from multiprocessing.get_context('spawn') and keep all process creation inside if __name__ == '__main__':.
Short answer
As of November 2026, target Python 3.15.2, October 25 release.
Choose / don't choose
- spawn: choose isolation from parent locks/connections; don't send an open client or a local closure as a startup argument.
- forkserver: choose supported POSIX deployments that accept its server/temporary-directory lifecycle; don't assume every system provides it.
- Library code: choose a caller-provided context; don't reset the application's global start method (context contract).
Not for: cancellation and coroutine cleanup; see python-coroutine-never-awaited-and-fastapi-async-def-blocking.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
multiprocessing.get_context | 'spawn' | Python current docs | API |
Pool(initializer=..., initargs=...) | Each worker calls initializer(*initargs) when it starts; defaults None, () | Python current docs | Pool, implementation |
multiprocessing.set_start_method | 'spawn', once under main guard, before first worker/context use; default force=False | Python current docs | API |
get_start_method | Return None if not yet selected and allow_none=True; ordinary default allow_none=False selects/returns the platform default | Python current docs | API |
Pool(..., maxtasksperchild=...) | Default None; a positive count recycles workers and reruns their initializer | Python current docs | Pool, implementation |
Platform boundary: current Windows/macOS use spawn. The version 3.14 change changed eligible POSIX defaults from fork to forkserver. Since version 3.15.1, current docs specify spawn instead when no writable temporary directory exists. Inspect get_all_start_methods() and the selected method on each deployment (current defaults).
Order of operations
1. Keep import time limited to definitions/constants. Put CLI parsing, scheduler startup, migration execution and pool creation under the main guard; spawn reimports the main module. 2. Choose a local context, or set the global method once inside that guard before starting workers. Create queues/locks from the same context. 3. Define targets and initializers at importable module scope. Pass configuration data; have each initializer open that worker's connection/client. Parent-only actions remain under the guard. Each replacement worker runs initialization again (Pool lifecycle). 4. Close intake, finish accepted tasks, then close and join the pool. Do not rely on garbage collection for pool lifetime; a pool context exits through termination rather than promising a graceful drain (Pool).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
Can't pickle local object | Nested target in the reported fresh-process creation | Move target to an importable module; pass serializable data and create resources in initializer | dated user report, lambda report |
Can't get attribute 'f' | Children require an importable __main__ module | Run an importable script/module and guard process creation | documented example |
The local-target report shows how a default-method change exposes a pre-existing portability assumption. Repair the target/arguments first. Ronald Oussoren's explanation explains that fork can cause hard crashes on macOS.
Missing
- The linked source confirms: spawn uses a fresh interpreter, safe import requirements and picklable startup inputs.
- Frozen executable behavior and available start methods depend on the deployment; this card assumes an importable Python module, not a packaged standalone binary.
- Initializer idempotency and external resource shutdown remain application responsibilities.
Checked
2026-11-12; current runtime and boundary release pages, multiprocessing reference and both dated issue reports read. No worker or multiprocessing commands executed.
Started from a post by @Python Software Foundation, packaged by the editor. Original post