def _autoreload(self): th = self.scheduler.setTimer(self.checkinterval, self.checkinterval) try: tm = TimerEvent.createMatcher(th) while True: yield (tm,) t = time() reloads = [] loaded = self.activeModules().values() self._logger.debug('Checking loaded modules: %r', loaded) for k in loaded: p, _ = findModule(k, False) if not p or not hasattr(p, '__file__') or not p.__file__: continue if p.__file__.endswith('.pyc'): source = p.__file__[:-1] else: source = p.__file__ try: mtime = os.path.getmtime(source) if mtime <= t and mtime > self._lastcheck: reloads.append(k) except: pass if reloads: self._logger.warning('Auto reload following modules: %r', reloads) try: for m in self.reloadmodules(reloads): yield m except: self._logger.warning('Exception occurs on auto reload', exc_info=True) self._lastcheck = t finally: self.scheduler.cancelTimer(th)
def setUp(self): #self.scheduler logging.basicConfig() self.scheduler = Scheduler(DefaultPolling()) #self.scheduler.debugging = True #self.scheduler.logger.setLevel('DEBUG') #Client.logger.setLevel('DEBUG') import tests import os.path rootpath, _ = os.path.split(tests.__path__[0]) if rootpath: os.chdir(rootpath) self.scheduler.queue.addSubQueue(3, PollEvent.createMatcher(category=PollEvent.WRITE_READY), 'write', None, None, CBQueue.AutoClassQueue.initHelper('fileno')) self.scheduler.queue.addSubQueue(1, PollEvent.createMatcher(category=PollEvent.READ_READY), 'read', None, None, CBQueue.AutoClassQueue.initHelper('fileno')) self.scheduler.queue.addSubQueue(5, PollEvent.createMatcher(category=PollEvent.ERROR), 'error') self.scheduler.queue.addSubQueue(2, ConnectionControlEvent.createMatcher(), 'control') self.scheduler.queue.addSubQueue(4, ConnectionWriteEvent.createMatcher(), 'connectionwrite', 40, 40, CBQueue.AutoClassQueue.initHelper('connection')) self.scheduler.queue.addSubQueue(10, RoutineControlEvent.createMatcher(), 'routine') self.scheduler.queue.addSubQueue(9, TimerEvent.createMatcher(), 'timer') self.scheduler.queue.addSubQueue(8, ResolveResponseEvent.createMatcher(), 'resolve') self.scheduler.queue.addSubQueue(8, ResolveRequestEvent.createMatcher(), 'resolvereq') self.scheduler.queue.addSubQueue(20, SystemControlEvent.createMatcher(), 'sysctl') self.scheduler.queue.addSubQueue(0, SystemControlLowPriorityEvent.createMatcher(), 'sysctllow') #Client.logger.setLevel('DEBUG') #TcpServer.logger.setLevel('DEBUG') self.protocolServer = TestProtocol(True) self.protocolClient = TestProtocol(False) self.resolver = Resolver(scheduler=self.scheduler) self.resolver.start()
def _autoreload(self): th = self.scheduler.setTimer(self.checkinterval, self.checkinterval) try: tm = TimerEvent.createMatcher(th) while True: yield (tm,) t = time() reloads = [] loaded = self.activeModules().values() self._logger.debug("Checking loaded modules: %r", loaded) for k in loaded: p, _ = findModule(k, False) if not p or not hasattr(p, "__file__") or not p.__file__: continue if p.__file__.endswith(".pyc"): source = p.__file__[:-1] else: source = p.__file__ try: mtime = os.path.getmtime(source) if mtime <= t and mtime > self._lastcheck: reloads.append(k) except: pass if reloads: self._logger.warning("Auto reload following modules: %r", reloads) try: for m in self.reloadmodules(reloads): yield m except: self._logger.warning("Exception occurs on auto reload", exc_info=True) self._lastcheck = t finally: self.scheduler.cancelTimer(th)
async def task(): th = container.scheduler.setTimer(interval, interval) tm = TimerEvent.createMatcher(th) try: while True: await tm self.cleanup() finally: container.scheduler.cancelTimer(th)
def task(): th = container.scheduler.setTimer(interval, interval) tm = TimerEvent.createMatcher(th) try: while True: yield (tm,) self.cleanup() finally: container.scheduler.cancelTimer(th)
def _sampleroutine(self): th = self.scheduler.setTimer(self.interval, self.interval) try: tm = TimerEvent.createMatcher(th) lt = time() t = 0 interval = self.interval while True: yield (tm, ) ct = time() tc = 0 if self.connections: for connection, lc in list(self.connections.items()): if self.server: cc = connection.totalrecv else: cc = connection.totalsend speed = (cc - lc) / (ct - lt) tc += (cc - lc) unit = '' if speed > 1024.0: speed /= 1024.0 unit = 'K' if speed > 1024.0: speed /= 1024.0 unit = 'M' if speed > 1024.0: speed /= 1024.0 unit = 'G' print( '%s\t%.1fs - %.1fs\t%.2f%sB/s(%.2f%sbit/s)' % (self.identifiers[connection], t * interval, (t + 1) * interval, speed, unit, speed * 8, unit)) self.connections[connection] = cc if len(self.connections) > 1: speed = tc / (ct - lt) unit = '' if speed > 1024.0: speed /= 1024.0 unit = 'K' if speed > 1024.0: speed /= 1024.0 unit = 'M' if speed > 1024.0: speed /= 1024.0 unit = 'G' print( '[SUM]\t%.1fs - %.1fs\t%.2f%sB/s(%.2f%sbit/s)' % (t * interval, (t + 1) * interval, speed, unit, speed * 8, unit)) lt = ct t += 1 finally: self.scheduler.cancelTimer(th)
async def _timeout(self): th = self.scheduler.setTimer(self.checkinterval, self.checkinterval) try: tm = TimerEvent.createMatcher(th) while True: await tm t = time() timeouts = [ k for k, v in self.db.items() if v[1] is not None and v[1] < t ] for k in timeouts: del self.db[k] finally: self.scheduler.cancelTimer(th)
def _sampleroutine(self): th = self.scheduler.setTimer(self.interval, self.interval) try: tm = TimerEvent.createMatcher(th) lt = time() t = 0 interval = self.interval while True: yield (tm,) ct = time() tc = 0 if self.connections: for connection,lc in list(self.connections.items()): if self.server: cc = connection.totalrecv else: cc = connection.totalsend speed = (cc - lc) / (ct - lt) tc += (cc - lc) unit = '' if speed > 1024.0: speed /= 1024.0 unit = 'K' if speed > 1024.0: speed /= 1024.0 unit = 'M' if speed > 1024.0: speed /= 1024.0 unit = 'G' print('%s\t%.1fs - %.1fs\t%.2f%sB/s(%.2f%sbit/s)' % (self.identifiers[connection], t * interval, (t+1) * interval, speed, unit, speed * 8, unit)) self.connections[connection] = cc if len(self.connections) > 1: speed = tc / (ct - lt) unit = '' if speed > 1024.0: speed /= 1024.0 unit = 'K' if speed > 1024.0: speed /= 1024.0 unit = 'M' if speed > 1024.0: speed /= 1024.0 unit = 'G' print('[SUM]\t%.1fs - %.1fs\t%.2f%sB/s(%.2f%sbit/s)' % (t * interval, (t+1) * interval, speed, unit, speed * 8, unit)) lt = ct t += 1 finally: self.scheduler.cancelTimer(th)
def __init__(self): ''' Constructor ''' if hasattr(self, 'logging'): if isinstance(self.logging, dict): logging_config = dict(self.logging) else: logging_config = self.logging.todict() logging_config.setdefault('disable_existing_loggers', False) logging.config.dictConfig(logging_config) elif hasattr(self, 'loggingconfig'): logging.config.fileConfig(self.loggingconfig, disable_existing_loggers=False) self.scheduler = Scheduler(DefaultPolling(), getattr(self, 'processevents', None), getattr(self, 'queuedefaultsize', None), getattr(self, 'queuemaxsize', None), defaultQueueClass=CBQueue.AutoClassQueue.initHelper('_classname0'), defaultQueuePriority = 400) if self.debugging: self.scheduler.debugging = True self.scheduler.logger.setLevel(logging.DEBUG) self.scheduler.queue.addSubQueue(self.pollwritepriority, PollEvent.createMatcher(category=PollEvent.WRITE_READY), 'write', None, None, CBQueue.AutoClassQueue.initHelper('fileno')) self.scheduler.queue.addSubQueue(self.pollreadpriority, PollEvent.createMatcher(category=PollEvent.READ_READY), 'read', None, None, CBQueue.AutoClassQueue.initHelper('fileno')) self.scheduler.queue.addSubQueue(self.pollerrorpriority, PollEvent.createMatcher(category=PollEvent.ERROR), 'error') self.scheduler.queue.addSubQueue(self.connectioncontrolpriority, ConnectionControlEvent.createMatcher(), 'control') self.scheduler.queue.addSubQueue(self.connectionwritepriority, ConnectionWriteEvent.createMatcher(), 'connectionwrite', self.totalwritelimit, self.totalwritelimit, CBQueue.AutoClassQueue.initHelper('connection', self.preservefornew, subqueuelimit = self.writelimitperconnection)) self.scheduler.queue.addSubQueue(self.streamdatapriority, StreamDataEvent.createMatcher(), 'streamdata', self.streamdatalimit, self.streamdatalimit, CBQueue.AutoClassQueue.initHelper('stream', self.preservefornew, subqueuelimit = self.datalimitperstream)) self.scheduler.queue.addSubQueue(self.routinecontrolpriority, RoutineControlEvent.createMatcher(), 'routine') self.scheduler.queue.addSubQueue(self.timerpriority, TimerEvent.createMatcher(), 'timer') self.scheduler.queue.addSubQueue(self.resolverresppriority, ResolveResponseEvent.createMatcher(), 'resolve') self.scheduler.queue.addSubQueue(self.resolverreqpriority, ResolveRequestEvent.createMatcher(), 'resolvereq', 16) self.scheduler.queue.addSubQueue(self.sysctlpriority, SystemControlEvent.createMatcher(), 'sysctl') self.scheduler.queue.addSubQueue(self.sysctllowpriority, SystemControlLowPriorityEvent.createMatcher(), 'sysctllow') self.scheduler.queue.addSubQueue(self.moduleapicallpriority, ModuleAPICall.createMatcher(), 'moduleapi', None, None, CBQueue.AutoClassQueue.initHelper('target', 2, subqueuelimit = 5)) self.scheduler.queue.addSubQueue(self.moduleapireplypriority, ModuleAPIReply.createMatcher(), 'moduleapireply') self.scheduler.queue.addSubQueue(self.modulenotifypriority, ModuleNotification.createMatcher(), 'modulenotify', None, None, CBQueue.AutoClassQueue.initHelper('target', subqueuelimit=5)) self.scheduler.queue.addSubQueue(self.moduleloadeventpriority, ModuleLoadStateChanged.createMatcher(), 'moduleload') self.scheduler.queue.addSubQueue(self.lockpriority, LockEvent.createMatcher(), 'lock', None, None, CBQueue.AutoClassQueue.initHelper('key', subqueuelimit=1)) self.scheduler.queue.addSubQueue(self.futurepriority, FutureEvent.createMatcher(), 'future') self.resolver = Resolver(self.scheduler, self.resolverpoolsize) self.moduleloader = ModuleLoader(self)
def __init__(self): ''' Constructor ''' if hasattr(self, 'logging'): if isinstance(self.logging, dict): logging_config = dict(self.logging) else: logging_config = self.logging.todict() logging_config.setdefault('disable_existing_loggers', False) logging.config.dictConfig(logging_config) elif hasattr(self, 'loggingconfig'): logging.config.fileConfig(self.loggingconfig, disable_existing_loggers=False) self.scheduler = Scheduler(DefaultPolling(), getattr(self, 'processevents', None), getattr(self, 'queuedefaultsize', None), getattr(self, 'queuemaxsize', None), defaultQueueClass=CBQueue.AutoClassQueue.initHelper('_classname0'), defaultQueuePriority = 400) if self.debugging: self.scheduler.debugging = True self.scheduler.logger.setLevel(logging.DEBUG) self.scheduler.queue.addSubQueue(self.pollwritepriority, PollEvent.createMatcher(category=PollEvent.WRITE_READY), 'write', None, None, CBQueue.AutoClassQueue.initHelper('fileno')) self.scheduler.queue.addSubQueue(self.pollreadpriority, PollEvent.createMatcher(category=PollEvent.READ_READY), 'read', None, None, CBQueue.AutoClassQueue.initHelper('fileno')) self.scheduler.queue.addSubQueue(self.pollerrorpriority, PollEvent.createMatcher(category=PollEvent.ERROR), 'error') self.scheduler.queue.addSubQueue(self.connectioncontrolpriority, ConnectionControlEvent.createMatcher(), 'control') self.scheduler.queue.addSubQueue(self.connectionwritepriority, ConnectionWriteEvent.createMatcher(), 'connectionwrite', self.totalwritelimit, self.totalwritelimit, CBQueue.AutoClassQueue.initHelper('connection', self.preservefornew, subqueuelimit = self.writelimitperconnection)) self.scheduler.queue.addSubQueue(self.streamdatapriority, StreamDataEvent.createMatcher(), 'streamdata', self.streamdatalimit, self.streamdatalimit, CBQueue.AutoClassQueue.initHelper('stream', self.preservefornew, subqueuelimit = self.datalimitperstream)) self.scheduler.queue.addSubQueue(self.routinecontrolpriority, RoutineControlEvent.createMatcher(), 'routine') self.scheduler.queue.addSubQueue(self.timerpriority, TimerEvent.createMatcher(), 'timer') self.scheduler.queue.addSubQueue(self.resolverresppriority, ResolveResponseEvent.createMatcher(), 'resolve') self.scheduler.queue.addSubQueue(self.resolverreqpriority, ResolveRequestEvent.createMatcher(), 'resolvereq', 16) self.scheduler.queue.addSubQueue(self.sysctlpriority, SystemControlEvent.createMatcher(), 'sysctl') self.scheduler.queue.addSubQueue(self.sysctllowpriority, SystemControlLowPriorityEvent.createMatcher(), 'sysctllow') self.scheduler.queue.addSubQueue(self.moduleapicallpriority, ModuleAPICall.createMatcher(), 'moduleapi', None, None, CBQueue.AutoClassQueue.initHelper('target', 2, subqueuelimit = 5)) self.scheduler.queue.addSubQueue(self.moduleapireplypriority, ModuleAPIReply.createMatcher(), 'moduleapireply') self.scheduler.queue.addSubQueue(self.modulenotifypriority, ModuleNotification.createMatcher(), 'modulenotify', None, None, CBQueue.AutoClassQueue.initHelper('target', subqueuelimit=5)) self.scheduler.queue.addSubQueue(self.moduleloadeventpriority, ModuleLoadStateChanged.createMatcher(), 'moduleload') self.scheduler.queue.addSubQueue(self.lockpriority, LockEvent.createMatcher(), 'lock', None, None, CBQueue.AutoClassQueue.initHelper('key', subqueuelimit=1)) self.resolver = Resolver(self.scheduler, self.resolverpoolsize) self.moduleloader = ModuleLoader(self)