views:

490

answers:

1

Hi, I am having trouble with the python multiprocessing module. I am using the Process() class to spawn a new process in order to utilize my second core. This second process loads a bunch of data into RAM and than waits patiently instead of consuming.

Anyway, I just wanted to see what that process printed with the "print" command; however I do not see anything that it prints; I only see what the parent process prints. Now this makes sense to me since they live in two different process. The second process doesn't spawn its own shell/standard output window, nor is its output sent to the parent; yet when this process crashs, it prints everything that my script told it to print plus the stack trace and error.

I am wondering if their is a simple way to send the child process' prints to the first or have it spawn a shell/standard output so that I may debug it. I know I could create a multiprocessing.Queue dedicated to transmitting prints to the parent so that it may print these to standard output; but I do not feel like doing this if a simpler solution exists.

P.S. multiprocessing is great, but like most modules, it needs more documentation!

Thx

+6  A: 

Have you tried flushing stdout?

import sys
print "foo"
sys.stdout.flush()
orip
awesome! Yeah that really worked. What does this sys.stdout.flush() do? Does it explicitly force the prints to stdout? Thx again!
It clears the buffers. Stdout is saved until you have a full buffer. Flush stops waiting. Try printing to stderr to see what it does. Sometimes it is unbuffered.
S.Lott
@Nicholas: no problem :) When running subprocesses their standard output is usually buffered (and their standard error usually isn't), S.Lott explained what that means. You can try different tricks to prevent buffering, but remembering to use sys.stdout.flush() is simple and always works.
orip