Skip to content

Instantly share code, notes, and snippets.

@alexeygrigorev
Created July 16, 2018 08:35
Show Gist options
  • Save alexeygrigorev/01ce847f2e721b513b42ea4a6c96905e to your computer and use it in GitHub Desktop.
Save alexeygrigorev/01ce847f2e721b513b42ea4a6c96905e to your computer and use it in GitHub Desktop.
Python stdout sharing between chind & parent processes
import sys
import time
from io import StringIO
import subprocess
from multiprocessing import Process, Pipe
from threading import Thread
# "framework" code
class TeeOut(StringIO):
def __init__(self, pipe, std=sys.__stdout__):
self.pipe = pipe
def write(self, s):
self.pipe.send(s.strip())
def run_capturing_process(pipe, target, args):
sys.stdout = TeeOut(pipe, std=sys.__stdout__)
sys.stderr = TeeOut(pipe, std=sys.__stderr__)
target(*args)
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
def print_stdout(pipe):
while True:
try:
recv = pipe.recv().strip()
if recv:
print(recv)
except EOFError:
print('>> process done >>')
break
def capturing_run(target, args):
parent_pipe, child_pipe = Pipe()
process = Process(target=run_capturing_process, args=(child_pipe, target, args))
process.start()
stdthread = Thread(target=print_stdout, args=(parent_pipe, ))
stdthread.start()
process.join()
parent_pipe.close()
child_pipe.close()
# user defined code
def run_process(message):
for i in range(message['range']):
print(i)
command = 'echo "from shell: %d"' % i
exit_code = subprocess.call(command, shell=True)
time.sleep(message['sleep'])
def run(message):
capturing_run(target=run_process, args=(message, ))
if __name__ == '__main__':
message = {'range': 10, 'sleep': .1}
run(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment