4

I have this file (it doesn't make any useful work, it's only for learning):

import multiprocessing,sys
def parent(numproc=2):
    print ('at start')
    childs=[]
    print ('bfore Pipe')
    (parentEnd,childEnd)=multiprocessing.Pipe()
    i=0
    print ('printing i:',i)
    child=multiprocessing.Process(target=child_proc, args=(childEnd,i))
    print ('created child')
    child.start()
    print ('started child')
    print ('joining child')
    child.join()
    print ('joined child')
    print ('exeted from for i in childs')
    mins=[1,2]
    print ('task ended. result: ',min(mins))
def child_proc(pipe,name):
    pass
if __name__ == '__main__':
    parent()

in this form it runs perfectly:

at start
bfore Pipe
printing i: 0
created child
started child
joining child
joined child
exeted from for i in childs
task ended. result:  1

but if I put in end of file instead of

if __name__ == '__main__':
    parent()

only

parent()

it falls in cycle...

at start
bfore Pipe
printing i: 0
created child
started child
joining child
at start
bfore Pipe
printing i: 0
created child
started child
joining child
at start
bfore Pipe
printing i: 0
created child
started child
joining child
Traceback (most recent call last):

Why?! What different makes this if clause?

Ishayahu
  • 349
  • 1
  • 19
  • check what the `if __name__` condition mean... http://stackoverflow.com/questions/419163/what-does-if-name-main-do – avasal Jul 16 '12 at 09:07

2 Answers2

6

This is an issue with multiprocessing on MS Windows: the main module is imported by the child tasks, so any code not protected by the if __name__ . . . clause gets run again, resulting in an infinite loop.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
2

The subprocess has the following __name__: __parents_main__ and not __main__ any more. This is why your process does not loop when you are testign the __name__ variable.

For more information on this have a look to the chapter Safe importing of main module

gecco
  • 17,969
  • 11
  • 51
  • 68