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()
async def main(self): import os self.resolving = set() if hasattr(os, 'fork'): fork = True else: fork = False mp = self.mp and fork (process, queue, pipein, outqueue) = self._createobjs(fork, mp) try: process.start() self.scheduler.registerPolling(pipein, POLLING_IN, True) response_matcher = PollEvent.createMatcher(pipein.fileno(), PollEvent.READ_READY) error_matcher = PollEvent.createMatcher(pipein.fileno(), PollEvent.ERROR) control_matcher = ConnectorControlEvent.createMatcher(self) if self.allowcontrol: system_matchers = (response_matcher, error_matcher, control_matcher) else: system_matchers = (response_matcher, error_matcher) isFull = False isEOF = False while True: if not isEOF: if isFull or self.stopreceive: ev, m = await M_(*system_matchers) else: ev, m = await M_(*(tuple(self.matchers) + system_matchers)) if m is error_matcher: isEOF = True if isEOF: self.scheduler.unregisterPolling(pipein, self.jobs == 0) self.jobs = 0 pipein.close() pipein = None await self.wait_with_timeout(1) if mp: process.terminate() (process, queue, pipein, outqueue) = self._createobjs(fork, mp) process.start() self.scheduler.registerPolling(pipein, POLLING_IN, True) response_matcher = PollEvent.createMatcher(pipein.fileno(), PollEvent.READ_READY) error_matcher = PollEvent.createMatcher(pipein.fileno(), PollEvent.ERROR) if self.allowcontrol: system_matchers = (response_matcher, error_matcher, control_matcher) else: system_matchers = (response_matcher, error_matcher) isFull = False elif m is control_matcher: if ev.type == ConnectorControlEvent.ADDMATCHERS: for m in ev.matchers: self.matchers.add(m) elif ev.type == ConnectorControlEvent.REMOVEMATCHERS: for m in ev.matchers: self.matchers.discard(m) elif ev.type == ConnectorControlEvent.SETMATCHERS: self.matchers = set(ev.matchers) elif ev.type == ConnectorControlEvent.STOPRECEIVE: self.stopreceive = True else: self.stopreceive = False elif m is response_matcher: if mp: while pipein.poll(): try: events = pipein.recv() except EOFError: isEOF = True break self.sendevents(events) else: while True: try: if not pipein.recv(4096): isEOF = True break except socket.error as exc: if exc.errno == errno.EAGAIN or exc.errno == errno.EWOULDBLOCK: break elif exc.errno == errno.EINTR: continue else: isEOF = True break while True: try: events = outqueue.get(False) except Empty: break self.sendevents(events) isFull = False else: try: self.enqueue(queue, ev, m) except Full: isFull = True finally: if pipein is not None: self.scheduler.unregisterPolling(pipein, self.jobs == 0) pipein.close() if mp: process.terminate() else: queue.put(None)
def main(self): import os self.resolving = set() if hasattr(os, "fork"): fork = True else: fork = False mp = self.mp and fork (process, queue, pipein, outqueue) = self._createobjs(fork, mp) try: process.start() self.scheduler.registerPolling(pipein, POLLING_IN, True) response_matcher = PollEvent.createMatcher(pipein.fileno(), PollEvent.READ_READY) error_matcher = PollEvent.createMatcher(pipein.fileno(), PollEvent.ERROR) control_matcher = ConnectorControlEvent.createMatcher(self) if self.allowcontrol: system_matchers = (response_matcher, error_matcher, control_matcher) else: system_matchers = (response_matcher, error_matcher) isFull = False isEOF = False while True: if not isEOF: if isFull or self.stopreceive: yield system_matchers else: yield tuple(self.matchers) + system_matchers if self.matcher is error_matcher: isEOF = True if isEOF: self.scheduler.unregisterPolling(pipein, self.jobs == 0) self.jobs = 0 pipein.close() pipein = None for m in self.waitWithTimeout(1): yield m if mp: process.terminate() (process, queue, pipein, outqueue) = self._createobjs(fork, mp) process.start() self.scheduler.registerPolling(pipein, POLLING_IN, True) response_matcher = PollEvent.createMatcher(pipein.fileno(), PollEvent.READ_READY) error_matcher = PollEvent.createMatcher(pipein.fileno(), PollEvent.ERROR) if self.allowcontrol: system_matchers = (response_matcher, error_matcher, control_matcher) else: system_matchers = (response_matcher, error_matcher) isFull = False elif self.matcher is control_matcher: if self.event.type == ConnectorControlEvent.ADDMATCHERS: for m in self.event.matchers: self.matchers.add(m) elif self.event.type == ConnectorControlEvent.REMOVEMATCHERS: for m in self.event.matchers: self.matchers.discard(m) elif self.event.type == ConnectorControlEvent.SETMATCHERS: self.matchers = set(self.event.matchers) elif self.event.type == ConnectorControlEvent.STOPRECEIVE: self.stopreceive = True else: self.stopreceive = False elif self.matcher is response_matcher: if mp: while pipein.poll(): try: events = pipein.recv() except EOFError: isEOF = True break self.sendevents(events) else: while True: try: if not pipein.recv(4096): isEOF = True break except socket.error as exc: if exc.errno == errno.EAGAIN or exc.errno == errno.EWOULDBLOCK: break elif exc.errno == errno.EINTR: continue else: isEOF = True break while True: try: events = outqueue.get(False) except Empty: break self.sendevents(events) isFull = False else: try: self.enqueue(queue, self.event, self.matcher) except Full: isFull = True finally: if pipein is not None: self.scheduler.unregisterPolling(pipein, self.jobs == 0) pipein.close() if mp: process.terminate() else: queue.put(None)