Created
August 20, 2015 08:25
-
-
Save genzj/446a652746a381e16dff to your computer and use it in GitHub Desktop.
Output and merge subprocess outputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from time import sleep | |
for i in range(1, 3): | |
print "line %d" %(i, ) | |
print "xxxxxxxxxx" * 20 | |
sleep(5) | |
print "exit" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from subprocess import Popen, PIPE | |
from time import sleep | |
import sys | |
from Queue import Queue, Empty | |
from threading import Thread | |
def enqueue_output(out, queue): | |
for line in iter(out.readline, b''): | |
queue.put(line) | |
out.close() | |
p = Popen(['/public/bin/python', 'child.py'], stdout=PIPE, bufsize=1) | |
q = Queue() | |
t = Thread(target=enqueue_output, args=(p.stdout, q)) | |
t.daemon = True | |
t.start() | |
stdout_buf = [] | |
retcode = None | |
while True: | |
try: | |
line = q.get_nowait() | |
except Empty: | |
if retcode is not None: | |
# process exited and no more lines to process | |
break | |
else: | |
# wait a little while | |
sleep(1) | |
else: | |
sys.stdout.write(line) | |
stdout_buf += line | |
# simulate line processing | |
sleep(10) | |
retcode = p.poll() | |
print 'child exited with code: %d' % (retcode,) | |
all_stdout = ''.join(stdout_buf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment