python - redirect function's stdout on the fly -
suppose have python function foo. want run in background (using threading or multiprocessing), , have stdout , stderr available in real time produced foo. if foo shell command, accomplish using subprocess.popen , generator, described here, but need python function. multiprocessing module spawning new processes, seems hard child's stdout in real time.
p.s: need redirect function's stdout webpage generated, using server sent events protocol
for it's worth, think bad thought , should ideally rewrite foo function homecoming output in other way. said...
if foo standard pure-python function using standard python functions produce output (print , file.write), redirect standard output stringio or similar.
python 2.x example:
import sys, stringio def foo(): print "haha! can't capture output!" print>>sys.stderr, "... or this!" output = stringio.stringio() saved, sys.stdout = sys.stdout, output sys.stderr = output foo() sys.stdout = sys.stderr = saved print output.getvalue() python multithreading
No comments:
Post a Comment