Logging and threading

usenet at mulch.ca usenet at mulch.ca
Fri Mar 31 22:57:11 EST 2006


I'm having some problems getting the logging module to work with the
threading module.  I've narrowed the problem down to the following
code:

import logging, threading

update_log = logging.getLogger('update_log')
update_log.addHandler(logging.FileHandler("/tmp/update_log"))

class dlThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        ul = logging.getLogger('update_log')
        ul.warn("log this")
        print "finished"

t = dlThread()
t.start()

When executed, I receive the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python2.4/logging/__init__.py", line 735, in
emit
    self.stream.write(fs % msg)
ValueError: I/O operation on closed file

The exception isn't fatal though, as the print "finished" line is
executed.  Alternately, if I run dlThread.run directly everything works
as I would expect, albeit without the multithreading.  Since the
logging module is advertised as being threadsafe, I suspect there's
something basic I don't understand about threads.

-Alex




More information about the Python-list mailing list