Logging and threading

U

usenet

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
 
D

Dennis Lee Bieber

t = dlThread()
t.start()
t.join()
When executed, I receive the following exception:
Your main program (thread) is ending, closing out the log file, etc.
before the thread gets to run...
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top