logging module and threading

James T. Dennis jadestar at idiom.com
Wed Jun 13 02:28:04 CEST 2007

Ross Boylan <ross at biostat.ucsf.edu> wrote:
> I would like my different threads to log without stepping on each
> other.

> Past advice on this list (that I've found) mostly says to send the
> messages to a Queue.  That would work, but bypasses the logging
> module's facilities.

> The logging module itself is "thread-safe", but I think that just
> means that individual output is protected.  If I have, in temporarly
> sequence:
> thread 1: warning("A")
> thread 2: info("something")
> thread 1: warning("B")
> then I think I'll get them output in this order.  It's thread-safe in
> that the log will not end up with an entry like
> A
> some
> B
> thing
> (I think).  But I  want to get, for example,
> A
> B
> something

> What I would like is for each thread to emit a chunk of log messages
> when it finishes a unit of work.

 This sounds like a job for the Queue class/module to me.
 Could you create a Queue such that all your worker threads
 are producers to it and you have one dedicated thread as a
 consumer that relays log entries from the Queue into your loggers?

Jim Dennis,
Starshine: Signed, Sealed, Delivered

More information about the Python-list mailing list