5

I'm exploring the Python multiprocessing module and don't understand why the following code does not print anything at all. Without the while-loop the program prints Worker_1 as expected.

import multiprocessing, time

def worker1():
    print 'Worker_1'
    while 1:
        print 'Worker_1'
        time.sleep(3)
    return

if __name__ == '__main__':
    jobs = []
    p = multiprocessing.Process(target=worker1)
    jobs.append(p)
    p.start()
1
  • I'm not a master on the multiprocessing module, and I cant test it right now, but try adding p.join() after your p.start(). If the main program exits after the subprocess is started, does the subprocess continue? I don't think so, but I don't know. Note that this means you'll have to do some shenanigans to make the worker stop when you want it to. Alternatively, look at the fork module maybe. Commented Dec 4, 2010 at 21:38

2 Answers 2

2

On my system (Python 2.6&2.7 on Linux), this works as expected. Which platform are you using? On some platforms(Windows), fork hast to be emulated by creating a totally new process and setting it up. I suspect some stdout is not transferred to the child process. Try:

  • The threading module. It's sufficient if you just want to wait for an event in a thread.
  • Running your program on a POSIX-compatible platform, such as BSD, Linux or Solaris
  • Outputting to a file
Sign up to request clarification or add additional context in comments.

3 Comments

Currently I'm working with Eclipse and PyDev on Windows. Outputting to a file doesn't work either. I just get an empty file. What I don't understand is the significance of the while-loop. There is output without the loop, but no output with it.
Try adding sys.stdout.flush() maybe?
I can't believe that print("something") doesn't work in a child process on Windows. fork is not the reason. I've tried the ported to Python 3.4 code from the question and it produces the expected output with multiprocesssing.set_start_method('spawn') ('fork' naturally works too).
0

Are you using IDLE? I think that this is the problem. The code you have works for me in Linux when called from the command line (it prints 'Worker_1' every 3 seconds). When I try it in IDLE, it stops immediately. This is because you don't have the p.join() in there which results in the main process stopping right away which then stops the worker process. Apparently, p.join() isn't as necessary from the command line in Linux though I would recommend it anyway.

However, even with p.join() and using IDLE, the script just sits there and we don't see any text. This is, I think, because of the weird way that IDLE reroutes stdout to idlelib.rpc.RPCProxy. So, in other words, if you are using IDLE, then that is your problem.

1 Comment

I'm using Ecplise with PyDev on Windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.