Best way to capture output from an exec'ed (or such) script?

Gabriel Genellina gagsl-py2 at
Sun Aug 5 11:50:06 CEST 2007

En Sat, 04 Aug 2007 18:52:16 -0300, gregpinero at  
<gregpinero at> escribió:

>> On Aug 2, 7:32 pm, "Gabriel Genellina" <gagsl-... at> wrote:>  
>> If your web server is multithreaded (or you use some other way to  
>> process
>> > many simultaneous requests) you have to be more careful - remember  
>> that
>> > sys.stdout is global, you must find a way to distinguish between  
>> output
>> >  from different processes all going into the same collector.
> I'm actually worried about this now.  Does anyone know of any
> potential solutions?  Anything to at least get me started?

You can use threading.currentThread() to distinguish between threads, and  
a Lock (or RLock) to ensure the output doesn't get mixed:

def write(msg):
     t = time.strftime("%x %X", time.localtime())
     who = threading.currentThread().getName()
     line = "%s (%-15.15s) %s\n" % (t, who, msg)
     OutputDebugString("%s (%-15.15s) %s\n" % (t, who, msg))
         with logfile() as f:

Gabriel Genellina

More information about the Python-list mailing list