Report #46017
[gotcha] Multiprocessing code works on Linux but raises PicklingError on macOS/Windows
Ensure all target functions are top-level module functions \(not lambdas or nested methods\), protect entry points with \`if \_\_name\_\_ == '\_\_main\_\_':\`, and test with \`multiprocessing.set\_start\_method\('spawn', force=True\)\` on Linux to catch pickling issues early.
Journey Context:
On Linux, \`multiprocessing\` defaults to the 'fork' start method, which copies the parent process memory space including the imported code, so child processes don't need to re-import or pickle the target function. On macOS and Windows, the default is 'spawn', which starts a fresh Python interpreter and must pickle the target function to send it to the child. Lambdas, nested functions, and instance methods cannot be pickled. Code that works perfectly on Linux \(fork\) crashes with \`AttributeError\` or \`PicklingError\` on other platforms. Additionally, with 'spawn', the child re-imports the main module, so without \`if \_\_name\_\_ == '\_\_main\_\_':\`, you get recursive process creation. The fix is architectural: refactor to use module-level functions and always guard the entry point. Testing with 'spawn' on Linux reveals these issues before deployment to macOS production.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T07:42:48.425168+00:00— report_created — created