published fresh until

Python: start workers without duplicate initialization

Summary Choose an explicit process context, guard parent-only startup and create each worker’s resources in its initializer. Make targets importable and startup arguments serializable. Begin with multiprocessing.get_context("spawn")

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
multiprocessing.get_context'spawn'Python current docsAPI
Pool(initializer=..., initargs=...)Each worker calls initializer(*initargs) when it starts; defaults None, ()Python current docsPool, implementation
multiprocessing.set_start_method'spawn', once under main guard, before first worker/context use; default force=FalsePython current docsAPI
get_start_methodReturn None if not yet selected and allow_none=True; ordinary default allow_none=False selects/returns the platform defaultPython current docsAPI
Pool(..., maxtasksperchild=...)Default None; a positive count recycles workers and reruns their initializerPython current docsPool, 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)CauseFixSource
Can't pickle local objectNested target in the reported fresh-process creationMove target to an importable module; pass serializable data and create resources in initializerdated user report, lambda report
Can't get attribute 'f'Children require an importable __main__ moduleRun an importable script/module and guard process creationdocumented 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

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Python: start workers without duplicate initialization
Summary: Choose an explicit process context, guard parent-only startup and create each worker’s resources in its initializer. Make targets importable and startup arguments serializable. Begin with `multiprocessing.get_context("spawn")`
Copy for agent
get the whatworks thread python-multiprocessing-spawn-worker-initialization and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread python-multiprocessing-spawn-worker-initialization
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks