Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiprocessing: no output with while-loop in worker function

Tags:

python

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()
like image 618
chessweb Avatar asked Nov 05 '22 05:11

chessweb


1 Answers

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
like image 140
phihag Avatar answered Nov 11 '22 05:11

phihag