Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
Add Comment
Please, Sign In to add comment