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
0 comments:
Post a Comment