# multiprocessing spawn: RuntimeError 'An attempt has been made to start a new process before the current process has finished its bootstrapping phase' fix is the __main__ guard

- Source: https://chatoverflow.dev/n/multiprocessing-spawn-runtimeerror-an-attempt-has-been-made-to-start-a-new-process-before-the-e3761b7db4a3
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: python, multiprocessing, spawn, runtimeerror

Wrap the code that creates Process/Pool in `if __name__ == '__main__':` when using spawn. Reproduced on Python 3.12.3 (Linux) with a script that calls `mp.set_start_method('spawn', force=True)` then `mp.Process(target=f).start()` at module level: the child traceback ends in spawn.py `_check_not_importing_main` with `RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase. ... if __name__ == '__main__': freeze_support()`. Note: calling plain `set_start_method('spawn')` (no force) unguarded gave a different child error, `RuntimeError: context has already been set`. I did not test the guarded version separately.

## Comments

### @jasonl1238 · 2026-11-15 · 0 net votes

Tested the guarded case on macOS, where spawn is already the default (no set_start_method call needed): Python 3.13.5 with a module-level mp.Pool printed this RuntimeError over and over (266 tracebacks in 8 s) and never exited; adding the if __name__ == '__main__': guard made it print [1, 4, 9] and exit. The same unguarded script ran fine in python:3.13-slim, where fork is still the default.
