views:

173

answers:

2

I have a problem with the code below, and with any code that uses the print function in the child processes: I can't see any printed statements, even if I use sys.std[err|out].write('worker') in place of print.

This is the code (from the official python documentation):

from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

The output is blank.

Note: the following code, using the threading module, works (prints hello bob):

import threading

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = threading.Thread(target=f, args=('bob',))
    p.start()
    p.join()

Can you please point me to the solution? Thanks in advance.

+1  A: 

Try this:

from multiprocessing import Process
import sys

def f(name):
    print 'hello', name
    sys.stdout.flush()

...

AFAIK the standard output of processed spawned by the multiprocessing module is buffered, hence you will see the output only if the buffer becomes full or you explicitly flush sys.stdout.

Tamás
Thanks a lot Tamas, but this in my case didn't work...
John
Which platform are you on? The above works for me on Mac OS X; in fact, it works even without flushing sys.stdout.
Tamás
Tamas, the platform I'm working on is win32.By the way, until now I tried to run the code within IDLE. Today I tried to run it from the command line, using this syntax: python.exe my_prog.pyand it worked.Anybody knows why? And how can I display print output by running programs within IDLE?
John
Well, IDLE is a strange thing. In order to "capture" everything what you write using `print` statements or `sys.stdout.write`, IDLE "overrides" `sys.stdout` and replaces it with an object that passes everything back to IDLE so it can print it. I guess when you are starting a new process from `multiprocessing`, this hackery is not inherited by the child process, therefore you don't see anything in IDLE. But I'm just guessing here, I don't have a Windows machine at the moment to check it.
Tamás
A: 

The docs for multiprocessing clearly explain why this won't work!

"Note: Functionality within this package requires that the _main_ method be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter."

manifest
First of all thanks for the answer.What I did is to write the code in a file named "example.py" and to hit F5 to run it. Maybe this means "running code within IDLE"?PS: I am a python newbie, so sorry for any (excessively) obvious thing I may have stated here.
John
Ah, well yes IDLE with the ">>" prompt counts as an interactive interpreter. So multiprocessing basically won't work unless you write the code in a file and hit F5 as you say or type "python example.py" in the command prompt. I can understand how this might be a confusing issue. You're welcome.
manifest