0

I want to learn multiprocessing in python. I started reading http://www.doughellmann.com/PyMOTW/multiprocessing/basics.html and I am not able to understand the section on importing target functions.

In particular what does the following sentence mean..

"Wrapping the main part of the application in a check for __main__ ensures that it is not run recursively in each child as the module is imported."

Can someone explain this in more detail with an example ?

Graddy
  • 2,828
  • 5
  • 19
  • 25

4 Answers4

0
"""This is my module (mymodule.py)"""

def sum(a,b):
    """>>> sum(1,1)
       2
       >>> sum(1,-1)
       0
    """
    return a+b

# if you run this module using 'python mymodule.py', run a self test
# if you just import this module, you get foo() and other definitions,
# but the self-test isn't run
if __name__=='__main__':
    import doctest
    doctest.testmod()
Useless
  • 64,155
  • 6
  • 88
  • 132
0

Ensures that the script being run is in the 'top level environment' for interactivity.

For example if you wanted to interact with the user (launching process) you would want to ensure that it is main.

if __name__ == '__main__':
    do_something()
lukecampbell
  • 14,728
  • 4
  • 34
  • 32
0

On Windows, the multiprocessing module imports the __main__ module when spawning a new process. If the code that spawns the new process is not wrapped in a if __name__ == '__main__' block, then importing the main module will again spawn a new process. And so on, ad infinitum.

This issue is also mentioned in the multiprocessing docs in the section entitled "Safe importing of main module". There, you'll find the following simple example:

Running this on Windows:

from multiprocessing import Process

def foo():
    print 'hello'

p = Process(target=foo)
p.start()

results in a RuntimeError.

And the fix is to use:

if __name__ == '__main__':    
    p = Process(target=foo)
    p.start()
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677