how can a child thread notify a parent thread its status?

scriptlearner at scriptlearner at
Sun Jul 26 03:03:37 CEST 2009

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
                 entry = self.q.get()
            except Empty:
                 self.logger.debug('cant find any log entry')
                 self.logger.error("Unexpected error:", sys.exc_info()
            #do whatever that should be done"logger thread done") #should see this
message in log file as well
    def off(self):'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 #off is called successfully

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 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?

More information about the Python-list mailing list