I would like to redirect stdout
and stderr
of a new process back to the parent process via pipe.
I have found this code in another thread , which uses the os.dup2()
function to duplicate stdout
.
import os
from multiprocessing import Process, Pipe
def spam(w):
os.dup2(w.fileno(), 1)
for i in range(3):
print('eggs')
if __name__ == '__main__':
r, w = Pipe()
reader = os.fdopen(r.fileno(), 'r')
p = Process(target=spam, args=(w,))
p.start()
for i in range(3):
print('From pipe: %s' % reader.readline())
reader.close()
p.join()
With Python 2.7 everything works fine, but Python 3 gives me a invalid file descriptor error after the main process has read the messages from the pipe. I cannot figure out why this happens. I tried closing the file handler explicitly, but it did not work.
(I'm running this on a Linux machine.)
So, as it turns out not closing reader
works fine.
import os
from multiprocessing import Process, Pipe
def spam(w):
os.dup2(w.fileno(), 1)
for i in range(3):
print('eggs')
if __name__ == '__main__':
r, w = Pipe()
reader = os.fdopen(r.fileno(), 'r')
p = Process(target=spam, args=(w,))
p.start()
for i in range(3):
print('From pipe: %s' % reader.readline())
p.join()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.