python: multiprocessing.Pipe and redirecting stdout

自古美人都是妖i 提交于 2019-12-11 22:04:56

问题


I am using multiprocessing package to spawn a second process from which I would like to redirect stdout and stderr into the first process. I am using multiprocessing.Pipe object:

dup2(output_pipe.fileno(), 1)

Where output_pipe is an instance of multiprocessing.Pipe. However, when I try to read on the other end, it just hangs. I tried reading using Pipe.recv_bytes with a limit, but that raises an OSError. Is this possible at all or should I just switch to some lower level pipe functions?


回答1:


After experimenting in Python 2.7 I got this working example. With os.dup2 pipe's file descriptor is copied to standard output file descriptor, and each print function ends up writing to a pipe.

import os
import multiprocessing


def tester_method(w):
    os.dup2(w.fileno(), 1)

    for i in range(3):
        print 'This is a message!'


if __name__ == '__main__':
    r, w = multiprocessing.Pipe()

    reader = os.fdopen(r.fileno(), 'r')

    process = multiprocessing.Process(None, tester_method, 'TESTER', (w,))
    process.start()

    for i in range(3):
        print 'From pipe: %s' % reader.readline()

    reader.close()
    process.join()

Output:

From pipe: This is a message!

From pipe: This is a message!

From pipe: This is a message!


来源:https://stackoverflow.com/questions/48713062/python-multiprocessing-pipe-and-redirecting-stdout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!