Multithreading Embedded C/Python : Giving each thread its own sys.stdout
Hrvoje Niksic
hniksic at iskon.hr
Wed Jun 28 05:47:48 EDT 2000
"Warren Postma" <embed at NOSPAM.geocities.com> writes:
> I am trying to set up an embedded Python as a multithreaded
> environment where each thread gets it's own namespace, and in each
> namespace, sys.stdout has been separately redirected. I am very
> close. The problem is, it appears that the command "print" actually
> checks __main__.sys.stdout, not whatever namespace's sys.stdout you
> are currently in.
...
> It's like there can only be one sys.stdout instance, not one sys
> module per namespace, each with it's own sys.stdout object. What
> can I do?
You can take a different strategy: instead of giving threads separate
namespaces with multiple hacked `sys.stdout' objects, you could have
only *one* hacked sys.stdout object that checks which thread it's in
and calls its `write' method. For instance:
class HackedStdout:
def write(arg):
thread_stdout_objs[threading.currentThread].write(arg)
sys.stdout = HackedStdout
Now all you need to do at thread-creation time is set up the thread's
"stdout" object:
thread_stdout_objs[newthread] = <code>
...where <code> is the incantation that you now use to create the
thread-specific sys.stdout object.
Hope this helps.
More information about the Python-list
mailing list