RuntimeError on windows trying python multiprocessing RuntimeError on windows trying python multiprocessing windows windows

RuntimeError on windows trying python multiprocessing


On Windows the subprocesses will import (i.e. execute) the main module at start. You need to insert an if __name__ == '__main__': guard in the main module to avoid creating subprocesses recursively.

Modified testMain.py:

import parallelTestModuleif __name__ == '__main__':        extractor = parallelTestModule.ParallelExtractor()    extractor.runInParallel(numProcesses=2, numThreads=4)


Try putting your code inside a main function in testMain.py

import parallelTestModuleif __name__ ==  '__main__':  extractor = parallelTestModule.ParallelExtractor()  extractor.runInParallel(numProcesses=2, numThreads=4)

See the docs:

"For an explanation of why (on Windows) the if __name__ == '__main__' part is necessary, see Programming guidelines."

which say

"Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process)."

... by using if __name__ == '__main__'


Though the earlier answers are correct, there's a small complication it would help to remark on.

In case your main module imports another module in which global variables or class member variables are defined and initialized to (or using) some new objects, you may have to condition that import in the same way:

if __name__ ==  '__main__':  import my_module