Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

threads (specifically timers) and releasing resources

0 views
Skip to first unread message

Alex Hall

unread,
Mar 24, 2010, 10:31:03 PM3/24/10
to python-list
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. My question is: what happens with resources taken
up by previous timers, whether they cancel or execute? I would hate
for my program to run for a long time and slowly build up a massive
list of old threads (timers), taking up resources which should have
been released. Will Python perform GC and completely erase a thread
once it is done, or will it keep hanging around? If the latter, is
there a way to completely destroy a thread? Thanks; Google was not too
helpful on this one!

--
Have a great day,
Alex (msg sent from GMail website)
meh...@gmail.com; http://www.facebook.com/mehgcap

Tim Golden

unread,
Mar 25, 2010, 5:57:23 AM3/25/10
to python-list
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

Alex Hall

unread,
Mar 25, 2010, 10:38:36 AM3/25/10
to Tim Golden, python-list
Thanks, this should work.

> --
> http://mail.python.org/mailman/listinfo/python-list

0 new messages