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)