How to run a repeating timer every n minutes?

Frank Millman frank at chagford.com
Fri Oct 30 03:14:41 EDT 2009


Diez B. Roggisch wrote:
> mk wrote:
>
>>
>> I'm newbie at threading, so I'm actually asking: should not method like
>> stop() be surrounded with acquire() and release() of some threading.lock?
>>
>> I mean, is this safe to update running thread's data from the main
>> thread without lock?
>
> stop() is part of the Timer-interface, and tas it's not mentioned to be
> unsafe in the docs you can just call it. It might be that it internally
> calls some threadsafe means of communication (Event, Lock).
>

Thought I should jump in here to avoid any confusion.

mk is referring to the Timer class that I included in my reply to the OP. It 
is different from the Timer class in the threading module. I had not 
realised that I was duplicating an existing name - sorry about that.

The standard Timer class has a method called cancel(). I am sure that Diez's 
comments above will apply to this.

I included a method called stop() in my Timer class, and I think it is to 
this that mk is referring.

The method looks like this -

    def stop(self):
        self.event.set()

Is this threadsafe? I would have thought the answer is yes, but I am no 
expert. Perhaps someone else can confirm.

Thanks

Frank Millman






More information about the Python-list mailing list