Reputation: 18289
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?
Upvotes: 6
Views: 8291
Reputation: 26900
Sub-processes do not enter the if __name__ == '__main__'
clause. That way we can guarantee set_start_method
will be called only once.
It does not need to be specifically at the module level, but it can be in any function that is ran through that clause, and still guarantee only one invocation.
Upvotes: 5