multiprocessing and __main__

I

is un

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
 
I

is un

Me again, fix a type in my question:
So my question is what actually happens when I call p.start()? Is the
*entire* file reloaded under a different module name?
 
G

Gabriel Genellina

Trying the multiprocessing module for the first time, I spent quite a
bit on making this code run:
[...]
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?

The multiprocessing module handles multiple (separate) processes. On
Windows, a new Python interpreter is launched; on other OSs a different
approach is used. In any case, you get distinct and separate processes,
each one with its own set of loaded modules, etc.
 
I

iu2

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top