multiprocessing and __main__

is un israel.unterman at gmail.com
Sun Jul 26 07:52:36 CEST 2009


Hi,

Trying the multiprocessing module for the first time, I spent quite a
bit on making this code run:

from multiprocessing import Process
import time

def my_process():
    i = 0
    while 1:
        print i
        i += 1
        time.sleep(0.5)


p = Process(target=my_process, args=())
p.start()

print 'Process started'

The result was not what I expected, it seems like the process restarts
all the time, and the message 'Process started' keeps getting
printed...

Going back to the documentation, I realized that the doc was really
serious about the line:
if __name__ == '__main__' .. which I always ignore, and by changing
the code to

if __name__ == '__main__':
    p = Process(target=my_process, args=())
    p.start()

    print 'Process started'

the problem was solved.
So my question is what actually happens when I call p.start()? Is the
entry file reloaded under a different module name?

Thanks
iu2



More information about the Python-list mailing list