multiprocessing and __main__

iu2 israelu at elbit.co.il
Sun Jul 26 05:50:15 EDT 2009


On Jul 26, 8:52 am, is un <israel.unter... at gmail.com> wrote:
> 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

Ok, I found it, it is nicely documented, I missed it before. Case
closed.



More information about the Python-list mailing list