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

scriptlearner at gmail.com scriptlearner at gmail.com
Sat Jul 25 21:03:37 EDT 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
            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?



More information about the Python-list mailing list