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

gregpinero at gmail.com gregpinero at gmail.com
Sun Aug 5 18:56:31 CEST 2007


On Aug 5, 5:50 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
> En Sat, 04 Aug 2007 18:52:16 -0300, gregpin... at gmail.com
> <gregpin... 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


Thanks, Gabriel.  I'm not actually making the threads myself though.
Rather it's a simple single threaded script running under FastCGI.
Would your method still work?  I don't know too much about how FastCGI
works.  Perhaps that's where I should start.

-Greg




More information about the Python-list mailing list