what thread-synch mech to use for clean exit from a thread

Steven D'Aprano steve at pearwood.info
Mon Jul 15 05:10:01 CEST 2013

Oh, I forgot another comment...

On Mon, 15 Jul 2013 03:04:14 +0000, Steven D'Aprano wrote:

> On Mon, 15 Jul 2013 10:27:45 +0800, Gildor Oronar wrote:

>>        while time.time() - self.rate_timestamp < 5*3600:
>>           ... # update exchange rate
>>           if success:
>>              self.rate_timestamp == time.time()
>>           time.sleep(60)
>>        this.callback() # rate not updated 5 hours, a crisis
> I think that a cleaner way is to just set a flag on the thread instance.
> Initiate it with:
>     self.updates_seen = True
> in the __init__ method, and then add this after the while loop:
>     self.updates_seen = False

Sorry, I forgot to mention... I assume that the intention is that if the 
thread hasn't seen any updates for five hours, it should set the flag, 
and then *keep going*. Perhaps the rate will start updating again later.

If the intention is to actually close the thread, then there's no reason 
for an extra flag. Just exit the run() method normally, the thread will 
die, and you can check the thread's status with the is_alive() method.


More information about the Python-list mailing list