Python 3.4+multiprocessingfunction
multiprocessing.set_start_method()
start methods
Sets the global start method for child processes: 'spawn', 'fork' or 'forkserver'. Call it at most once, under if __name__ == '__main__'.
Syntax
multiprocessing.set_start_method(method, force=False)
A second call raises RuntimeError "context has already been set", even if it was not you but get_start_method() or creating a Queue that fixed the method. force=True overrides it, but that signals a global setting is the wrong tool: in a library or importable code use get_context('spawn'). Called outside the __main__ guard under spawn, it also runs in every child, raising RuntimeError again.
Examples
import multiprocessing as mp
if __name__ == '__main__':
mp.set_start_method('spawn')
print(mp.get_start_method()) # → spawn
try:
mp.set_start_method('spawn')
except RuntimeError as e:
print(e) # → context has already been set
mp.set_start_method('spawn', force=True)
print('forced') # → forced
See also
Open in the full glossarydocs.python.orgReport an inaccuracy