0

I've been trying to print to screen stuff from the process spawned by Pool. Unfortunately nothing comes up! I need this, as I'll have 4 processes running at the same time for quite a long, and I need to get some estimated and print some information meanwhile.

I've been browsing around trying to figured out by myself, but I haven't understand if this is really possible or how to do it. Using Process rather than Pool? Any help & code will be really appreciated!

Here is a very simple example I'll be happy to make it work!

from multiprocessing import Pool
import os

def printer(i):
    pid = os.getpid()
    print "#%i PID: %i" % (i,pid)
    return pid

if __name__ == '__main__':
    pool = Pool(2)
    res = pool.map(printer,[1,2])
    pool.terminate()
    print "PIDs", res

I would like this to print what "printer" does before terminate the pool and print "res".

Alessandro Mariani
  • 1,181
  • 2
  • 10
  • 26

1 Answers1

0

You can use Pool.imap to iterate over results as they get ready.

from multiprocessing import Pool
import os

def printer(i):
    pid = os.getpid()
    print "#%i PID: %i" % (i,pid)
    return pid

def print_result(res):
    print "PID", res

if __name__ == '__main__':
    pool = Pool(2)
    res = pool.imap(printer,range(10), 1)
    for pid in res:
        print pid
    pool.terminate()
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
  • Hi Daniel, what I need to print is what "printer" will print normally. If you run printer(1) you'll get something like "#1 PID: 4300". This is what I need! – Alessandro Mariani Jan 15 '14 at 16:33