3

If I have the following code:

def f():
    print 'ok!'
    import sys
    sys.exit()

if __name__=='__main__':
    import billiard
    billiard.forking_enable(0)
    p = billiard.Process( target=f)
    p.start()
    while p.is_alive():
        pass

The script behaves as expected, printing "ok!" and ending. But if I omit the if __name__=='__main__': line and de-indent the following lines, my machine (OS X) goes crazy, continually spawning tons of Python processes until I killall Python. Any idea what's going on here?

(To those marking this as a duplicate, note that while the other question asks the purpose of if __name__=='__main__' generally, I'm specifically asking why failure to use it here causes dramatically unexpected behaviour)

Mike Lawrence
  • 1,641
  • 5
  • 20
  • 40
  • Presumably, `billiard` is importing your code when run, which (if the guard is omitted) causes `billiard` to run again, importing your module, causing `billiard` to run again, importing... – jonrsharpe Oct 31 '14 at 17:56

1 Answers1

8

You're disabling fork support with the line:

billiard.forking_enable(0)

That means that the library will need to spawn (instead of fork) your child process, and have it re-import the __main__ module to run f, just like Windows does. Without the if __name__ ... guard, re-importing the __main__ module in the children will also mean re-running your code that creates the billiard.Process, which creates an infinite loop.

If you leave fork enabled, the re-import in the child process isn't necessary, so everything works fine with or without the if __name__ ... guard.

dano
  • 91,354
  • 19
  • 222
  • 219