Skip to content

Instantly share code, notes, and snippets.

@genzj
Created August 20, 2015 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genzj/446a652746a381e16dff to your computer and use it in GitHub Desktop.
Save genzj/446a652746a381e16dff to your computer and use it in GitHub Desktop.
Output and merge subprocess outputs
#!/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"
#!/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