Running python 2.7 on windows 7 (64bit).
When reading the docs for library module multiprocessing, it states several times the importance of the __main__ module, including the conditional (especially in Windows):
if __name__ == "__main__":
# create Process() here
My understanding, is that you don’t want to create Process() instances in the global namespace of the module (because when the child process imports the module, he will spawn yet another inadvertently).
I do not have to place Process managers at the very top level of my package execution hierarchy though (execution in the PARENT). As long as my Process()’s are created, managed, and terminated in a class method, or even in a function closure. Just not in the toplevel module namespace.
Am I understanding this warning/requirement correctly?
EDIT
After the first two responses, I add this quotation. This is in the introduction for Section 16.6 multiprocessing from the 2.7 docs.
Note: Functionality within this package requires that the
__main__module be importable by the children. This is covered in Programming
guidelines however it is worth pointing out here.This means that some
examples, such as themultiprocessing.Poolexamples will not work in
the interactive interpreter…
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You do not have to call Process() from the “top level” of the module.
It is perfectly fine to call Process from a class method.
The only caveat is that you can not allow Process() to be called if or when the module is imported.
Since Windows has no fork, the multiprocessing module starts a new Python process and imports the calling module. If Process() gets called upon import, then this sets off an infinite succession of new processes (or until your machine runs out of resources). This is the reason for hiding calls to Process() inside
if __name__ == "__main__"
since statements inside this if-statement will not get called upon import.
Method 2
__name__ is only ever equal to "__main__" if the script has been executed directly, either via python foo.py or python -m foo. This ensures that Process() will not be called if the script is imported as a module instead.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0