If you want to spawn a process, instead of fork a process, using multiprocess module, you can use the following command:
multiprocessing.set_start_method('spawn')
The documentation states:
To select a start method you use the set_start_method() in the if
__name__ == '__main__'clause of the main module
However, I've found that I can call set_start_method('spawn') outside of the if __name__ == '__main__' clause, and it still spawns instead of forks processes on Unix.
In fact, as long as you invoke set_start_method('spawn') prior to the first invocation of multiprocessing.Process(), it will behave as expected. If you instead attempt to call set_start_method('spawn') after the first multiprocessing.Process(), it seems to be ignored and will fork instead of spawn.
Why does the documentation state that set_start_method should be invoked inside the if __name__ == '__main__' clause?