Correctly reading stdout/stderr from subprocess
Maric Michaud
maric at aristote.info
Wed Jun 14 09:56:16 EDT 2006
Le Mercredi 14 Juin 2006 13:14, Maric Michaud a écrit :
> or use a
> threaded version
here it is.
I did it just to validate my point and because i don't use threads very often
in python, some exercises can't hurt :)
def run(command):
import subprocess
run = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# Wait for the process to return
import thread, threading
out, err = [], []
out_ended, err_ended = threading.Event(), threading.Event()
def getOutput(output, lines, ended_event) :
for i in output.readlines() : lines.append(i.rstrip('\n'))
ended_event.set()
out_thread = thread.start_new_thread(getOutput, (run.stdout, out,
out_ended))
err_thread = thread.start_new_thread(getOutput, (run.stderr, err,
err_ended))
out_ended.wait()
err_ended.wait()
returncode = run.wait()
return returncode, out, err
> (much more complicated).
isn't it.
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
More information about the Python-list
mailing list