redirecting sys.stdout and threads

Chris Liechti cliechti at gmx.net
Thu Nov 22 02:16:02 CET 2001


the standard way to redirect outputs of print statements is to assign an 
other file to sys.stdout and/or sys.stderr. the editors (idle, pythonwin, 
wxWindows pyshell, etc.) are an example for this.

but what can you do if more than one thread wants to use redirection?

i'm writing a server that writes it's messages on the screen (original 
sys.stdxxx). but the same server is listening on a TCP port and can receive 
strings for execution in a rexec environment. i want that print statements 
that come from a remote command are captured and redirected to the client, 
but at the same time the main thread should continue to write out his 
messages on the screen.
(this is also a problem when i want to run two interactive prompts in the 
same interpreter.)

i've attached a sample script that shows my problem.
thread 1 and 3 should write to stdout, but thread 2 should write to a 
buffer. the buffer is printed at the end, after all threads have finished 
and a line was printed.

i tested it on python 2.0 and 2.1 on win32, with negative results.

-- 
Chris <cliechti at gmx.net>

 



More information about the Python-list mailing list