我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用asyncore.ExitNow()。
def __init__(self, iterable, item_callback, done_callback, unwind_stack = True, pop_list = False): assert not pop_list or isinstance(iterable, list), "iterable must be a list when using pop_list" self.item_callback = item_callback self.done_callback = done_callback if done_callback is not None else lambda: None self.caller_file, self.caller_line, self.caller_function = traceback.extract_stack(limit = 2)[0][0:3] self.unwind_stack = unwind_stack self.pop_list = pop_list try: if self.pop_list: self.iterator = iterable else: self.iterator = iter(iterable) except (ExitNow, SystemExit): raise except Exception: logger.debug("Problem constructing iterator for %s", repr(iterable)) raise self.doit()
def __call__(self, *args, **kwargs): def thunk(): try: self.func(self.cb, self.eb, *args, **kwargs) except ExitNow: raise except Exception, e: self.eb(e) event_defer(thunk) if self.disable_signal_handlers: event_loop(catch_signals = ()) else: event_loop() if not self.fin: logger.warning("%r event_loop terminated without callback or errback", self) if self.err is None: return self.res elif isinstance(self.err, tuple): raise self.err[0], self.err[1], self.err[2] else: raise self.err
def handle_read_event(self): raise asyncore.ExitNow()
def test_readwriteexc(self): # Check exception handling behavior of read, write and _exception # check that ExitNow exceptions in the object handler method # bubbles all the way up through asyncore read/write/_exception calls tr1 = exitingdummy() self.assertRaises(asyncore.ExitNow, asyncore.read, tr1) self.assertRaises(asyncore.ExitNow, asyncore.write, tr1) self.assertRaises(asyncore.ExitNow, asyncore._exception, tr1) # check that an exception other than ExitNow in the object handler # method causes the handle_error method to get called tr2 = crashingdummy() asyncore.read(tr2) self.assertEqual(tr2.error_handled, True) tr2 = crashingdummy() asyncore.write(tr2) self.assertEqual(tr2.error_handled, True) tr2 = crashingdummy() asyncore._exception(tr2) self.assertEqual(tr2.error_handled, True) # asyncore.readwrite uses constants in the select module that # are not present in Windows systems (see this thread: # http://mail.python.org/pipermail/python-list/2001-October/109973.html) # These constants should be present as long as poll is available
def writable(self): if not self.shd["loop"]: raise asyncore.ExitNow('Connector is quitting!') if not self.ciao_queue.empty() and not self.write_pending: return True return False
def runq(cls): """ Run the timer queue: for each timer whose call time has passed, pull the timer off the queue and call its handler() method. Comparisions are made against time at which this function was called, so that even if new events keep getting scheduled, we'll return to the I/O loop reasonably quickly. """ now = rpki.sundial.now() while timer_queue and now >= timer_queue[0].when: t = timer_queue.pop(0) if cls.run_debug: logger.debug("Running %r", t) try: if t.handler is not None: t.handler() else: logger.warning("Timer %r expired with no handler set", t) except (ExitNow, SystemExit): raise except Exception, e: if t.errback is not None: t.errback(e) else: logger.exception("Unhandled exception from timer %r", t)
def test_readwrite(self): # Check that correct methods are called by readwrite() attributes = ('read', 'expt', 'write', 'closed', 'error_handled') expected = ( (select.POLLIN, 'read'), (select.POLLPRI, 'expt'), (select.POLLOUT, 'write'), (select.POLLERR, 'closed'), (select.POLLHUP, 'closed'), (select.POLLNVAL, 'closed'), ) class testobj: def __init__(self): self.read = False self.write = False self.closed = False self.expt = False self.error_handled = False def handle_read_event(self): self.read = True def handle_write_event(self): self.write = True def handle_close(self): self.closed = True def handle_expt_event(self): self.expt = True def handle_error(self): self.error_handled = True for flag, expectedattr in expected: tobj = testobj() self.assertEqual(getattr(tobj, expectedattr), False) asyncore.readwrite(tobj, flag) # Only the attribute modified by the routine we expect to be # called should be True. for attr in attributes: self.assertEqual(getattr(tobj, attr), attr==expectedattr) # check that ExitNow exceptions in the object handler method # bubbles all the way up through asyncore readwrite call tr1 = exitingdummy() self.assertRaises(asyncore.ExitNow, asyncore.readwrite, tr1, flag) # check that an exception other than ExitNow in the object handler # method causes the handle_error method to get called tr2 = crashingdummy() self.assertEqual(tr2.error_handled, False) asyncore.readwrite(tr2, flag) self.assertEqual(tr2.error_handled, True)
def event_loop(catch_signals = (signal.SIGINT, signal.SIGTERM)): """ Replacement for asyncore.loop(), adding timer and signal support. """ old_signal_handlers = {} while True: save_sigs = len(old_signal_handlers) == 0 try: for sig in catch_signals: old = signal.signal(sig, _raiseExitNow) if save_sigs: old_signal_handlers[sig] = old while asyncore.socket_map or timer_queue: t = timer.seconds_until_wakeup() if debug_event_timing: logger.debug("Dismissing to asyncore.poll(), t = %s, q = %r", t, timer_queue) asyncore.poll(t, asyncore.socket_map) timer.runq() if timer.gc_debug: gc.collect() if gc.garbage: for i in gc.garbage: logger.debug("GC-cycle %r", i) del gc.garbage[:] except ExitNow: break except SystemExit: raise except ValueError, e: if str(e) == "filedescriptor out of range in select()": logger.error("Something is badly wrong, select() thinks we gave it a bad file descriptor.") logger.error("Content of asyncore.socket_map:") for fd in sorted(asyncore.socket_map.iterkeys()): logger.error(" fd %s obj %r", fd, asyncore.socket_map[fd]) logger.error("Not safe to continue due to risk of spin loop on select(). Exiting.") sys.exit(1) logger.exception("event_loop() exited with exception %r, this is not supposed to happen, restarting") except Exception, e: logger.exception("event_loop() exited with exception %r, this is not supposed to happen, restarting") else: break finally: for sig in old_signal_handlers: signal.signal(sig, old_signal_handlers[sig])