Logging thread with Queue and multiple threads to log messages

Floris Bruynooghe floris.bruynooghe at gmail.com
Mon Nov 10 08:08:52 EST 2008


Hi

On Nov 9, 8:28 pm, "scriptlear... at gmail.com" <scriptlear... at gmail.com>
wrote:
> I am trying to put up a queue (through a logging thread) so that all
> worker threads can ask it to log messages.

There is no need to do anything like this, the logging module is
thread safe and you can happily just create loggers in a thread and
use them, you can even use loggers that where created outside of the
thread.  We use logging from threads all the time and it works
flawlessly.

As mentioned by Vinay in your other thread the problem you have is
that your main thread exists before the worker threads, which makes
atexit run the exithandlers and logging registers the
logging.shutdown() function which does flush all logging buffers and
closes all handles (i.e. closes the logfile).  So as said before by
simply calling .join() on all the worker threads inside the main
thread your problem will be solved.  You might get away with making
your threads daemonic, but I can't guarentee you won't run into race
conditions in that case.  If  you want to be really evil you could get
into muddling with atexit...

Regards
Floris



More information about the Python-list mailing list