[ python-Bugs-995907 ] memory leak with theads and enhancement of the timer class

SourceForge.net noreply at sourceforge.net
Thu Jul 22 15:16:52 CEST 2004


Bugs item #995907, was opened at 2004-07-22 15:16
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=995907&group_id=5470

Category: Threads
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Tobias Haar (thaar)
Assigned to: Nobody/Anonymous (nobody)
Summary: memory leak with theads and enhancement of the timer class

Initial Comment:
If i use the Timer cyclically the  memory becomes
always less.


I found following problems::

1.) The thread is not clean deleted.=>
file:threading.py  class: Thread methode:__delete

del _active[_get_ident()] only delete the thead from
the list, not the thead self.  I think the call of the
destructor of the c++ based library need a explicit del.

The problem will be fixed with following lines:

    def __delete(self):
        _active_limbo_lock.acquire()
        t=_active[_get_ident()]
        del _active[_get_ident()]
        del t
        _active_limbo_lock.release()

2.) A cyclic timer is a needed feature and it should
not use a new thread every time.

So i made following enhancement (parameter cyclic) in the 
file:threading.py  class: _Timer  

class _Timer(Thread):
    """Call a function after a specified number of seconds:

    t = Timer(30.0, f, args=[], kwargs={})
    t.start()
    t.cancel() # stop the timer's action if it's still
waiting
    """

    def __init__(self, interval, function, cyclic=0,
args=[], kwargs={}):
        Thread.__init__(self)
        self.interval = interval
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.finished = Event()
        self.cyclic= Event()
        if cyclic:
            self.cyclic.set()

    def cancel(self):
        """Stop the timer if it hasn't finished yet"""
        self.cyclic.clear()
        self.finished.set()

    def run(self):
        flag =1
        while flag:
            self.finished.wait(self.interval)
            if not self.finished.isSet():
                self.function(*self.args, **self.kwargs)
            if not self.cyclic.isSet():
                self.finished.set()
                flag = 0         





----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=995907&group_id=5470


More information about the Python-bugs-list mailing list