Python multiprocessor for windows if __name__ == "__main__"

Running python 2.7 on windows 7 (64 bit).

When reading documents for the multiprocessing library module, it repeatedly indicates the importance of the __main__ module, including the conditional (especially on Windows):

 if __name__ == "__main__": # create Process() here 

I understand that you do not want to instantiate Process () in the global namespace of the module (because when a child process imports the module, it will be unpredictable one more by accident).

I do not need to place process managers at the highest level of the package execution hierarchy (execution in PARENT). While my Process () is being created, managed and terminated by a class method, or even when a function is closed. Just not in the top-level module namespace.

Do I understand this warning / requirement correctly?


EDIT

After the first two answers, I add this quote. This is in the introduction for Development 16.6 of multiprocessing from 2.7 documents.

Note. The functionality in this package requires the __main__ module to be imported by children. This is described in the Program, however it is worth mentioning here. This means that some examples, such as multiprocessing.Pool examples, will not work in the interactive interpreter ...

+8
python multiprocessing
source share
2 answers

You do not need to call Process() from the "top level" of the module. It's pretty cool to call Process from a class method.

The only caveat is that you cannot resolve the Process() call if or when the module is imported.

Since Windows does not have a fork , the multiprocessing module starts a new Python process and imports the calling module. If Process() receives a call during import, this leads to an endless sequence of new processes (or until your machine runs out of resources). This causes the hidden calls to Process() inside

 if __name__ == "__main__" 

since the instructions inside this if-statement will not be called upon import.

+15
source share

__name__ always equal to "__main__" if the script was executed directly, either through python foo.py or python -m foo . This ensures that Process() will not be called if it is imported as a module instead of a script.

0
source share

All Articles