[email protected] wrote:
I decided to go with one big log file, which will be shared by all threads (child and parent). A log message Queue is used to store all log entries, and a customized logger thread will get log entries from the Queue.#from the logger thread# def run(self): while self.flag == 1: #if the flag is set to 0, the logger thread should exit try: entry = self.q.get() except Empty: self.logger.debug('cant find any log entry') continue except: self.logger.error("Unexpected error:", sys.exc_info() [0]) raise #do whatever that should be done self.logger.info("logger thread done") #should see this message in log file as well def off(self): self.logger.info('turning off flag') self.flag = 0 #in parent thread# logItemQ.put('We are done, lets stop the logger now.') time.sleep(1) #it seems that the logger thread cannot exit if I put a sleep here myLog.off() #off is called successfully myLog.join() I put an off method to turn off a flag so the logger thread knows it should exit. However, the last log message (the one 'We are done, lets stop the logger now.') won't be logged if I call myLog.off() and myLog.join() immediately. So, I put a time.sleep(1) to make sure the logger thread has enough time to finish it's job. Unfortunately, now the logger thread simply won't exit, and I don't see the message 'logger thread done'. I can't figure out at which point it hangs, since I don't any new log entry but the thread simply won't exit. Am I taking a right approach by using a flag? Should I lock the flag?
self.q.get() will block if the queue is empty, so the Empty exception is never raised. What's happening is that the parent thread puts the final message into the queue, sleeps, and then clears the flag; meanwhile, the logging thread gets the message, writes it out, checks the flag, which is still set, and then tries to get the next message. The queue is empty, so the .get() blocks. The simplest solution is not to use a flag, but the sentinel trick. The parent thread can put, say, None into the queue after the last message; when the logging thread gets None, it knows it should terminate. -- http://mail.python.org/mailman/listinfo/python-list
