3

How can I access the stdout of child processes prior to sending them to main process? I am using multiprocessing.Pool module to generate child process pools.

KT100
  • 1,381
  • 5
  • 17
  • 27
  • maybe this could help you: http://stackoverflow.com/questions/7714868/python-multiprocessing-how-can-i-reliably-redirect-stdout-from-a-child-process – Udy Oct 17 '13 at 07:41
  • @Udy the question you reference seems like a windows specific stdout redirection issue. Also, they're trying to redirect the stdout to a file. I on the other hand want to just modify the child process's stdout. – KT100 Oct 17 '13 at 08:00
  • true. but this will defantly help: http://stackoverflow.com/questions/4227808/how-can-i-send-python-multiprocessing-process-output-to-a-tkinter-gui – Udy Oct 17 '13 at 08:14

1 Answers1

9

The main process and the children all share the same standard input and standard output file descriptors. They have no control over what the other writes to them. The only thing you can do is replace stdin and stdout in the children with something else that the main process can control. As an example you could subclass a dummy file object like StringIO and redirect the data that the children write to this object to the parent via a Queue:

import sys
from multiprocessing import Queue, Pool, current_process
from StringIO import StringIO

class MyStringIO(StringIO):
    def __init__(self, queue, *args, **kwargs):
        StringIO.__init__(self, *args, **kwargs)
        self.queue = queue
    def flush(self):
        self.queue.put((current_process().name, self.getvalue()))
        self.truncate(0)

def initializer(queue):
     sys.stderr = sys.stdout = MyStringIO(queue)

def task(num):
     print num
     sys.stdout.flush()
     return num ** 2

q = Queue()
pool = Pool(3, initializer, [q])

for _ in pool.map(task, range(5)):
    proc, out = q.get()
    print proc, "got", out

This should print something like this:

PoolWorker-1 got 0
PoolWorker-1 got 3
PoolWorker-1 got 4
PoolWorker-2 got 1
PoolWorker-3 got 2

Don't forget to call sys.{stdout,stderr}.flush() at the end of task otherwise, nothing will be written to the queue.

Tanveer Alam
  • 5,185
  • 4
  • 22
  • 43
kouk
  • 1,453
  • 12
  • 12
  • the children's output is redirected to the `MyStringIO` object which then sends the data to the main process via the queue. The main process then does all the processing it needs and outputs it to `sys.stdout` (which, as I explain in my updated answer, is shared by all processes attached to the terminal) – kouk Oct 17 '13 at 09:14