thread specific sys.stdout?

Peter Hansen peter at
Wed Sep 15 23:24:22 CEST 2004

Diez B. Roggisch wrote:

> You could replace sys.stdout by a class that splits the written text
> depending on the current thread. It might look roughly like this:
> class ThreadPrinter:
>     def __init__(self):
>         _.fhs = {}
>     def write(self, value):
>         f = _.fhs.get(threading.currentThread(),
> open(get_some_nice_file_name(), "w")
>         f.write(value)
>         _.fhs[threading.currentThread()] = f

Have you run this code?  It looks to me suspiciously as
though it will raise an exception on the second write
call in any given thread, as the non-shortcircuiting call
to .get() tries to open the nice_file in write mode for
a second time.

Also, what's "_" supposed to be here?  self?


More information about the Python-list mailing list