close

[Solved] RuntimeError on windows trying python multiprocessing

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error RuntimeError on windows trying python multiprocessing in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How RuntimeError on windows trying python multiprocessing Error Occurs?

Today I get the following error RuntimeError on windows trying python multiprocessing in python.

How To Solve RuntimeError on windows trying python multiprocessing Error ?

  1. How To Solve RuntimeError on windows trying python multiprocessing Error ?

    To Solve RuntimeError on windows trying python multiprocessing Error 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.

  2. RuntimeError on windows trying python multiprocessing

    To Solve RuntimeError on windows trying python multiprocessing Error 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.

Solution 1

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 parallelTestModule

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

Solution 2

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

import parallelTestModule

if __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__'

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read