我们从Python开源项目中,提取了以下38个代码示例,用于说明如何使用twisted.internet.defer.AlreadyCalledError()。
def bootstrap(self, replace_if_newer=False): domain = self._domain self.log.debug('Bootstrapping provider %s' % domain) def first_bootstrap_done(ignored): try: self.first_bootstrap.callback('got config') except defer.AlreadyCalledError: pass def first_bootstrap_error(failure): self.first_bootstrap.errback(failure) return failure d = self.maybe_download_provider_info(replace=replace_if_newer) d.addCallback(self.maybe_download_ca_cert, replace_if_newer) d.addCallback(self.validate_ca_cert) d.addCallbacks(first_bootstrap_done, first_bootstrap_error) d.addCallback(self.maybe_download_services_config) self.ongoing_bootstrap = d
def datagramReceived(self, datagram, address): """ Receive response from Paxos Learners, match the response with the original request and pass it to the application handler. """ try: fmt = '>' + 'B {0}s'.format(VALUE_SIZE) packer = struct.Struct(fmt) packed_size = struct.calcsize(fmt) unpacked_data = packer.unpack(datagram[:packed_size]) req_id, result = unpacked_data self.defers[req_id].callback(result) pass except defer.AlreadyCalledError as ex: #logging.error("already call") pass
def test_raisesAfterCancelAndCallback(self): """ A L{defer.Deferred} without a canceller, when cancelled must allow a single extra call to callback, and raise L{defer.AlreadyCalledError} if callbacked or errbacked thereafter. """ d = defer.Deferred() d.addCallbacks(self._callback, self._errback) d.cancel() # A single extra callback should be swallowed. d.callback(None) # But a second call to callback or errback is not. self.assertRaises(defer.AlreadyCalledError, d.callback, None) self.assertRaises(defer.AlreadyCalledError, d.errback, Exception())
def test_raisesAfterCancelAndErrback(self): """ A L{defer.Deferred} without a canceller, when cancelled must allow a single extra call to errback, and raise L{defer.AlreadyCalledError} if callbacked or errbacked thereafter. """ d = defer.Deferred() d.addCallbacks(self._callback, self._errback) d.cancel() # A single extra errback should be swallowed. d.errback(Exception()) # But a second call to callback or errback is not. self.assertRaises(defer.AlreadyCalledError, d.callback, None) self.assertRaises(defer.AlreadyCalledError, d.errback, Exception())
def test_cancelQueueAfterGet(self): """ When canceling a L{Deferred} from a L{DeferredQueue} that does not have a result (i.e., the L{Deferred} has not fired), the cancel causes a L{defer.CancelledError} failure. If the queue has a result later on, it doesn't try to fire the deferred. """ queue = defer.DeferredQueue() d = queue.get() d.cancel() self.assertImmediateFailure(d, defer.CancelledError) def cb(ignore): # If the deferred is still linked with the deferred queue, it will # fail with an AlreadyCalledError queue.put(None) return queue.get().addCallback(self.assertIs, None) d.addCallback(cb) done = [] d.addCallback(done.append) self.assertEqual(len(done), 1)
def _fail(self, error): """ Errback all queued deferreds. """ if self._failed: # We're recursing; bail out here for simplicity return error self._failed = 1 if self.nextDeferred: try: self.nextDeferred.errback(failure.Failure(ConnectionLost('FTP connection lost', error))) except defer.AlreadyCalledError: pass for ftpCommand in self.actionQueue: ftpCommand.fail(failure.Failure(ConnectionLost('FTP connection lost', error))) return error
def testAlreadyCalled_CC(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._call_1(d) self.failUnlessRaises(defer.AlreadyCalledError, self._call_2, d)
def testAlreadyCalled_CE(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._call_1(d) self.failUnlessRaises(defer.AlreadyCalledError, self._err_2, d)
def testAlreadyCalled_EE(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._err_1(d) self.failUnlessRaises(defer.AlreadyCalledError, self._err_2, d)
def testAlreadyCalled_EC(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._err_1(d) self.failUnlessRaises(defer.AlreadyCalledError, self._call_2, d)
def testAlreadyCalledDebug_CC(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._call_1(d) try: self._call_2(d) except defer.AlreadyCalledError, e: self._check(e, "testAlreadyCalledDebug_CC", "_call_1", "_call_2") else: self.fail("second callback failed to raise AlreadyCalledError")
def testAlreadyCalledDebug_EC(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._err_1(d) try: self._call_2(d) except defer.AlreadyCalledError, e: self._check(e, "testAlreadyCalledDebug_EC", "_err_1", "_call_2") else: self.fail("second callback failed to raise AlreadyCalledError")
def testAlreadyCalledDebug_EE(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._err_1(d) try: self._err_2(d) except defer.AlreadyCalledError, e: self._check(e, "testAlreadyCalledDebug_EE", "_err_1", "_err_2") else: self.fail("second errback failed to raise AlreadyCalledError")
def testNoDebugging(self): defer.setDebugging(False) d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._call_1(d) try: self._call_2(d) except defer.AlreadyCalledError, e: self.failIf(e.args) else: self.fail("second callback failed to raise AlreadyCalledError")
def _run(self, methodName, result): from twisted.internet import reactor timeout = self.getTimeout() def onTimeout(d): e = defer.TimeoutError("%r (%s) still running at %s secs" % (self, methodName, timeout)) f = failure.Failure(e) # try to errback the deferred that the test returns (for no gorram # reason) (see issue1005 and test_errorPropagation in # test_deferred) try: d.errback(f) except defer.AlreadyCalledError: # if the deferred has been called already but the *back chain # is still unfinished, crash the reactor and report timeout # error ourself. reactor.crash() self._timedOut = True # see self._wait todo = self.getTodo() if todo is not None and todo.expected(f): result.addExpectedFailure(self, f, todo) else: result.addError(self, f) onTimeout = utils.suppressWarnings( onTimeout, util.suppress(category=DeprecationWarning)) if self._shared: test = self.__class__._testCaseInstance else: test = self method = getattr(test, methodName) d = defer.maybeDeferred(utils.runWithWarningsSuppressed, self.getSuppress(), method) call = reactor.callLater(timeout, onTimeout, d) d.addBoth(lambda x : call.active() and call.cancel() or x) return d
def _connect(self): deferreds = [] for i, remote in enumerate(self.remotes): d = defer.Deferred() deferreds.append(d) factory = vnc_client.client_factory(d, self.error_buffer) factory.rewarder_session = self factory.label = 'vnc:{}:{}'.format(i, remote) endpoint = endpoints.clientFromString(reactor, 'tcp:'+remote) def success(i): logger.info('[%s] VNC connection established', factory.label) def fail(reason): reason = error.Error('[{}] Connection failed: {}'.format(factory.label, reason.value)) try: d.errback(utils.format_error(reason)) except defer.AlreadyCalledError: pass endpoint.connect(factory).addCallback(success).addErrback(fail) d = defer.DeferredList(deferreds, fireOnOneErrback=True) def success(results): # Store the _clients list when connected self._clients = [client for success, client in results] d.addCallback(success) return d
def _error(self, e): self.close() self.factory.error_buffer.record(e) if self.factory.deferred: try: self.factory.deferred.errback(utils.format_error(e)) except defer.AlreadyCalledError: pass
def handle_iopub_msg(msg): if msg["msg_type"] == "status": if msg["content"]["execution_state"] == "idle": parent_id = msg["parent_header"]["msg_id"] try: status_msg_cache[parent_id].callback(True) except AlreadyCalledError: pass reactor.callLater(1.0, clean_cache, status_msg_cache, key=parent_id) if "idle" in status_msg_cache: if not status_msg_cache["idle"].called: status_msg_cache["idle"].callback(True)
def _handle_interrupt_status(self): self._kernel_event_dispatcher.on_interrupt_fail(self._interrupt_timeout, "NA") for key in [k for k in status_msg_cache.keys() if k != "idle"]: try: status_msg_cache[key].callback(True) except AlreadyCalledError: status_msg_cache.__delitem__(key)
def _with_log(op, res): """ The default behaviour on firing an already-fired Deferred is unhelpful for debugging, because the AlreadyCalledError can easily get lost or be raised in a context that results in a different error. So make sure it is logged (for the abstractions defined here). If we are in a test, log.err will cause the test to fail. """ try: op(res) except defer.AlreadyCalledError, e: print "err %r" % (repr(op),)
def testAlreadyCalled_CC(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._call_1(d) self.assertRaises(defer.AlreadyCalledError, self._call_2, d)
def testAlreadyCalled_CE(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._call_1(d) self.assertRaises(defer.AlreadyCalledError, self._err_2, d)
def testAlreadyCalled_EE(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._err_1(d) self.assertRaises(defer.AlreadyCalledError, self._err_2, d)
def testAlreadyCalled_EC(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._err_1(d) self.assertRaises(defer.AlreadyCalledError, self._call_2, d)
def testAlreadyCalledDebug_CC(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._call_1(d) try: self._call_2(d) except defer.AlreadyCalledError as e: self._check(e, "testAlreadyCalledDebug_CC", "_call_1", "_call_2") else: self.fail("second callback failed to raise AlreadyCalledError")
def testAlreadyCalledDebug_EC(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._err_1(d) try: self._call_2(d) except defer.AlreadyCalledError as e: self._check(e, "testAlreadyCalledDebug_EC", "_err_1", "_call_2") else: self.fail("second callback failed to raise AlreadyCalledError")
def testAlreadyCalledDebug_EE(self): d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._err_1(d) try: self._err_2(d) except defer.AlreadyCalledError as e: self._check(e, "testAlreadyCalledDebug_EE", "_err_1", "_err_2") else: self.fail("second errback failed to raise AlreadyCalledError")
def testNoDebugging(self): defer.setDebugging(False) d = defer.Deferred() d.addCallbacks(self._callback, self._errback) self._call_1(d) try: self._call_2(d) except defer.AlreadyCalledError as e: self.assertFalse(e.args) else: self.fail("second callback failed to raise AlreadyCalledError")
def _run(self, methodName, result): from twisted.internet import reactor timeout = self.getTimeout() def onTimeout(d): e = defer.TimeoutError("%r (%s) still running at %s secs" % (self, methodName, timeout)) f = failure.Failure(e) # try to errback the deferred that the test returns (for no gorram # reason) (see issue1005 and test_errorPropagation in # test_deferred) try: d.errback(f) except defer.AlreadyCalledError: # if the deferred has been called already but the *back chain # is still unfinished, crash the reactor and report timeout # error ourself. reactor.crash() self._timedOut = True # see self._wait todo = self.getTodo() if todo is not None and todo.expected(f): result.addExpectedFailure(self, f, todo) else: result.addError(self, f) onTimeout = utils.suppressWarnings( onTimeout, util.suppress(category=DeprecationWarning)) method = getattr(self, methodName) if inspect.isgeneratorfunction(method): exc = TypeError( '%r is a generator function and therefore will never run' % ( method,)) return defer.fail(exc) d = defer.maybeDeferred( utils.runWithWarningsSuppressed, self._getSuppress(), method) call = reactor.callLater(timeout, onTimeout, d) d.addBoth(lambda x : call.active() and call.cancel() or x) return d