threads (specifically timers) and releasing resources

Tim Golden mail at timgolden.me.uk
Thu Mar 25 10:57:23 CET 2010


On 25/03/2010 02:31, Alex Hall wrote:
> Okay, I have my program and it has three different modes (there will
> be more than that). Each mode will have a timer attached to it. If the
> mode remains active and the timer runs out, a function specific to
> that mode is called. If that mode is switched away from, however, the
> timer is canceled and a new timer is created for the mode to which the
> user just switched.

I assume you're using Python's threading.Timer objects as you'd discussed
those before. If so, that's basically a threading.Thread in disguise.
In which case, you're going to have to make sure it cleans up after itself,
releasing whatever resources it holds.

Python's reference-count semantics and cyclic gc will take care of
things in the normal way once the timer-thread has completed. But
you'll have to make sure it completes.

> If the latter, is there a way to completely destroy a thread?

No: in Python, a thread has to self-destruct. This is a relatively
FAQ and there are quite a few recipes around. Here's an example of
something which seems to be close to your current needs:

http://code.activestate.com/recipes/464959-resettable-timer-class/

TJG



More information about the Python-list mailing list