Suppose I have a python function foo
. I want to run it in the background (using threading
or multiprocessing
), and have all its stdout and stderr available in real time as it is produced by foo
. If foo
was a shell command, I could achieve this by using subprocess.Popen
and a generator, as described here, but I need to do this with a python function. The multiprocessing
module is good for spawning new processes, but it seems to be hard to get to child's stdout in real time.
P.S: I need this to redirect function's stdout to a webpage as it is generated, using Server Sent Events protocol