catch output of threads

Erik Max Francis max at alcyone.com
Fri Nov 29 18:50:55 EST 2002


William wrote:

> Is it possible to catch the output from print of differents threads ?
> In a single thread, i can change the value of sys.stdout... but with
> differents thread i don't know ?
> 
> My goal is to make a little server with multithread working on
> windows without calling an external interpreter (like CGIHTTPServer
> do).

For my templating system EmPy -- which is not threaded -- I was forced
to put together a system where a sys.stdout is replaced with a proxy
object that can be "pushed and popped" by each evaluation/execution that
might conceivably use sys.stdout or a print statement, so that the proxy
object would direct the output to the output stream of the appropriate
interpreter that was running.

Unfortunately, though, this doesn't lend itself to a multithreaded
environment (and is the primary reason why EmPy does not support
threading), since you could easily encounter deadlocks (if, say, one
thread stays in an execution loop for an indefinite period).  I don't
think there's any way to do what you want short of modifying the Python
interpreter.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ In wildness is the preservation of the world.
\__/ Henry David Thoreau
    Maths reference / http://www.alcyone.com/max/reference/maths/
 A mathematics reference.



More information about the Python-list mailing list