Just a blog to share my tricks, code snippets

Thursday, August 31, 2017

Python: Some restrictions when using multiprocessing library on windows


I ran into one of these restrictions today and want to share it. Those restrictions are stated in multiprocessing library document at HERE . However, I want to explain them more and give some examples so we can understand them fully.

Restriction 1: Safe importing of main module


  • Problem:


Running the script below would fail with a RuntimeError

The error should be like this


  • Solution:


In the python document above, it states:
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).
That means we need to put the all the commands inside a if __name__ == '__main__': block. That will prevent calling a new process (could be unintended) when import that module.
The code should be look like this

Restriction 2: More picklability

Restriction 3: Global variables

Details of o other restrictions will be updated later whenever I have a chance :)

0 comments:

Post a Comment