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

Changeset 2434

Show
Ignore:
Timestamp:
06/14/09 16:39:17
Author:
fumanchu
Message:

python3: trap and log exceptions in PerpetualTimer? Monitor threads.

Files:

Legend:

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

    r2427 r2434  
    1717except ImportError: 
    1818    from sha import new as sha 
     19import sys 
    1920import time 
    2021import threading 
     
    158159            # so that tool config can be accessed inside the method. 
    159160            t = cherrypy.process.plugins.Monitor( 
    160                 cherrypy.engine, self.clean_up, self.clean_freq * 60) 
     161                cherrypy.engine, self.clean_up, self.clean_freq * 60, 
     162                name='Session cleanup') 
    161163            t.subscribe() 
    162164            cls.clean_thread = t 
     
    239241        """Clean up expired sessions.""" 
    240242        now = datetime.datetime.now() 
    241         for id, (data, expiration_time) in self.cache.items(): 
    242             if expiration_time < now: 
     243        for id, (data, expiration_time) in list(self.cache.items()): 
     244            if expiration_time <= now: 
    243245                try: 
    244246                    del self.cache[id] 
  • branches/python3/cherrypy/process/plugins.py

    r2412 r2434  
    383383            if self.finished.isSet(): 
    384384                return 
    385             self.function(*self.args, **self.kwargs) 
     385            try: 
     386                self.function(*self.args, **self.kwargs) 
     387            except Exception as x: 
     388                self.bus.log("Error in perpetual timer thread function %r." % 
     389                             self.function, level=40, traceback=True) 
     390                # Quit on first error to avoid massive logs. 
     391                raise 
    386392 
    387393 
     
    396402    frequency = 60 
    397403     
    398     def __init__(self, bus, callback, frequency=60): 
     404    def __init__(self, bus, callback, frequency=60, name=None): 
    399405        SimplePlugin.__init__(self, bus) 
    400406        self.callback = callback 
    401407        self.frequency = frequency 
    402408        self.thread = None 
     409        self.name = name 
    403410     
    404411    def start(self): 
    405412        """Start our callback in its own perpetual timer thread.""" 
    406413        if self.frequency > 0: 
    407             threadname = self.__class__.__name__ 
     414            threadname = self.name or self.__class__.__name__ 
    408415            if self.thread is None: 
    409416                self.thread = PerpetualTimer(self.frequency, self.callback) 
     417                self.thread.bus = self.bus 
    410418                self.thread.setName(threadname) 
    411419                self.thread.start() 
     
    418426        """Stop our callback's perpetual timer thread.""" 
    419427        if self.thread is None: 
    420             self.bus.log("No thread running for %s." % self.__class__.__name__) 
     428            self.bus.log("No thread running for %s." % self.name or self.__class__.__name__) 
    421429        else: 
    422430            if self.thread is not threading.currentThread(): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets