1

I am trying to use multiprocessing in python 2.7.7. I tried to implement this example:

import multiprocessing

def worker():
    """worker function"""
    print 'Worker'
    return

if __name__ == '__main__':
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker)
        jobs.append(p)
        p.start()

This code is from https://pymotw.com/2/multiprocessing/basics.html.

My problem is, that I can't see any output of the worker process. What is the problem here?

EDIT: My operating system is Windows 7 64bit.

EDIT: It is working, if I call the python-script from the windows command line (with python script.py). Previous I started the script from IDLE (Run Module (F5)) and this was not working.

wewa
  • 1,628
  • 1
  • 16
  • 35

1 Answers1

0

Try to call p.join() on those processes as per the examples in the official documentation: https://docs.python.org/2/library/multiprocessing.html

Depending on your OS and python implementation it may be because job control kills your sub-processes when the main process exits (I did not try).

cadrian
  • 7,332
  • 2
  • 33
  • 42
  • I tried `p.join()` but I doesn't change anything. But this is no way for me, because `p.join()` would block the calling/main-thread. – wewa Dec 22 '15 at 07:53
  • I don't think that sub-processes are killed because the main process exits. I tried to hold the main process open with a `while(1):` but this didn't change anything. – wewa Dec 22 '15 at 07:57
  • No problem. I was guessing. And with your edit (OS=Windows) the argument is moot anyway. – cadrian Dec 22 '15 at 08:26