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

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Aug 5 05:50:06 EDT 2007


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

>> On Aug 2, 7:32 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> 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))
     loglock.acquire()
     try:
         with logfile() as f:
             f.write(line)
     finally:
         loglock.release()

-- 
Gabriel Genellina




More information about the Python-list mailing list