Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1435

Show
Ignore:
Timestamp:
11/18/06 13:19:39
Author:
fumanchu
Message:

clean_interrupt was calling clean_thread.cancel() but not .join(). Session.clean_thread is also now a PerpetualTimer?, so it keeps the same thread rather than spawning new ones on each run.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/sessions.py

    r1426 r1435  
    2121import cherrypy 
    2222from cherrypy.lib import http 
     23 
     24 
     25class PerpetualTimer(threading._Timer): 
     26     
     27    def run(self): 
     28        while True: 
     29            self.finished.wait(self.interval) 
     30            if self.finished.isSet(): 
     31                return 
     32            self.function(*self.args, **self.kwargs) 
    2333 
    2434 
     
    5363                self.id = None 
    5464     
    55     def clean_cycle(self): 
    56         """Clean up expired sessions at regular intervals.""" 
    57         # clean_thread is both a cancelable Timer and a flag. 
    58         if self.clean_thread: 
    59             self.clean_up() 
    60             t = threading.Timer(self.clean_freq, self.clean_cycle) 
    61             self.__class__.clean_thread = t 
    62             t.start() 
    63      
    6465    def clean_interrupt(cls): 
    65         """Stop the expired-session cleaning cycle.""" 
     66        """Stop the expired-session cleaning timer.""" 
    6667        if cls.clean_thread: 
    6768            cls.clean_thread.cancel() 
     69            cls.clean_thread.join() 
    6870            cls.clean_thread = None 
    6971    clean_interrupt = classmethod(clean_interrupt) 
     
    111113        self.loaded = True 
    112114         
     115        # Stick the clean_thread in the class, not the instance. 
     116        # The instances are created and destroyed per-request. 
    113117        cls = self.__class__ 
    114118        if not cls.clean_thread: 
    115119            cherrypy.engine.on_stop_engine_list.append(cls.clean_interrupt) 
    116             # Use the instance to call clean_cycle so tool config 
    117             # can be accessed inside the method. 
    118             cls.clean_thread = t = threading.Timer(self.clean_freq, 
    119                                                    self.clean_cycle) 
     120            # clean_up is in instancemethod and not a classmethod, 
     121            # so tool config can be accessed inside the method. 
     122            t = PerpetualTimer(self.clean_freq, self.clean_up) 
     123            t.setName("CP Session Cleanup") 
     124            cls.clean_thread = t 
    120125            t.start() 
    121126     

Hosted by WebFaction

Log in as guest/cpguest to create tickets