我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用twisted.internet.defer.TimeoutError()。
def __call__(self, *args, **kwargs): while True: id = random.randrange(self.max_id) if id not in self.map: break def cancel(df): df, timer = self.map.pop(id) timer.cancel() try: df = defer.Deferred(cancel) except TypeError: df = defer.Deferred() # handle older versions of Twisted def timeout(): self.map.pop(id) df.errback(failure.Failure(defer.TimeoutError('in GenericDeferrer'))) self.on_timeout() timer = reactor.callLater(self.timeout, timeout) self.map[id] = df, timer self.func(id, *args, **kwargs) return df
def _collect_memory(self, netboxes): memory = dict() for mib in self._mibs_for_me(MEMORY_MIBS): try: mem = yield mib.get_memory_usage() except (TimeoutError, defer.TimeoutError): self._logger.debug("collect_memory: ignoring timeout in %s", mib.mib['moduleName']) else: if mem: self._logger.debug("Found memory values from %s: %r", mib.mib['moduleName'], mem) memory.update(mem) timestamp = time.time() result = [] for name, (used, free) in memory.items(): for netbox in netboxes: prefix = metric_prefix_for_memory(netbox, name) result.extend([ (prefix + '.used', (timestamp, used)), (prefix + '.free', (timestamp, free)), ]) defer.returnValue(result)
def test_downloadTimeout(self): """ If the timeout indicated by the C{timeout} parameter to L{client.HTTPDownloader.__init__} elapses without the complete response being received, the L{defer.Deferred} returned by L{client.downloadPage} fires with a L{Failure} wrapping a L{defer.TimeoutError}. """ self.cleanupServerConnections = 2 # Verify the behavior if no bytes are ever written. first = client.downloadPage( self.getURL("wait"), self.mktemp(), timeout=0.01) # Verify the behavior if some bytes are written but then the request # never completes. second = client.downloadPage( self.getURL("write-then-wait"), self.mktemp(), timeout=0.01) return defer.gatherResults([ self.assertFailure(first, defer.TimeoutError), self.assertFailure(second, defer.TimeoutError)])
def test_downloadTimeoutsWorkWithoutReading(self): """ If the timeout indicated by the C{timeout} parameter to L{client.HTTPDownloader.__init__} elapses without the complete response being received, the L{defer.Deferred} returned by L{client.downloadPage} fires with a L{Failure} wrapping a L{defer.TimeoutError}, even if the remote peer isn't reading data from the socket. """ self.cleanupServerConnections = 1 # The timeout here needs to be slightly longer to give the resource a # change to stop the reading. d = client.downloadPage( self.getURL("never-read"), self.mktemp(), timeout=0.05) return self.assertFailure(d, defer.TimeoutError)
def test_timeOut(self): """ Test the timeout on outgoing requests: when timeout is detected, all current commands fail with a L{TimeoutError}, and the connection is closed. """ d1 = self.proto.get(b"foo") d2 = self.proto.get(b"bar") d3 = Deferred() self.proto.connectionLost = d3.callback self.clock.advance(self.proto.persistentTimeOut) self.assertFailure(d1, TimeoutError) self.assertFailure(d2, TimeoutError) def checkMessage(error): self.assertEqual(str(error), "Connection timeout") d1.addCallback(checkMessage) self.assertFailure(d3, ConnectionDone) return gatherResults([d1, d2, d3])
def test_timeoutNotReset(self): """ Check that timeout is not resetted for every command, but keep the timeout from the first command without response. """ d1 = self.proto.get(b"foo") d3 = Deferred() self.proto.connectionLost = d3.callback self.clock.advance(self.proto.persistentTimeOut - 1) d2 = self.proto.get(b"bar") self.clock.advance(1) self.assertFailure(d1, TimeoutError) self.assertFailure(d2, TimeoutError) self.assertFailure(d3, ConnectionDone) return gatherResults([d1, d2, d3])
def test_waitUntilLockedWithTimeoutUnlocked(self): """ Test that a lock can be acquired while a lock is held but the lock is unlocked before our timeout. """ def onTimeout(f): f.trap(defer.TimeoutError) self.fail("Should not have timed out") self.assertTrue(self.lock.lock()) self.clock.callLater(1, self.lock.unlock) d = self.lock.deferUntilLocked(timeout=10) d.addErrback(onTimeout) self.clock.pump([1] * 10) return d
def test_errbackAddedBeforeTimeout(self): """ An errback added before a timeout is added errbacks with a L{defer.CancelledError} when the timeout fires. If the errback returns the L{defer.CancelledError}, it is translated to a L{defer.TimeoutError} by the timeout implementation. """ clock = Clock() d = defer.Deferred() dErrbacked = [None] def errback(f): dErrbacked[0] = f return f d.addErrback(errback) d.addTimeout(10, clock) clock.advance(15) self.assertIsInstance(dErrbacked[0], failure.Failure) self.assertIsInstance(dErrbacked[0].value, defer.CancelledError) self.failureResultOf(d, defer.TimeoutError)
def _ebRoundRobinBackoff(self, failure, fakeProto): failure.trap(defer.TimeoutError) # Assert that each server is tried with a particular timeout # before the timeout is increased and the attempts are repeated. for t in (1, 3, 11, 45): tries = fakeProto.queries[:len(self.testServers)] del fakeProto.queries[:len(self.testServers)] tries.sort() expected = list(self.testServers) expected.sort() for ((addr, query, timeout, id), expectedAddr) in zip(tries, expected): self.assertEqual(addr, (expectedAddr, 53)) self.assertEqual(timeout, t) self.assertFalse(fakeProto.queries)
def test_continuesWhenSomeRootHintsFail(self): """ The L{root.Resolver} is eventually created, even if some of the root hint lookups fail. Only the working root hint IP addresses are supplied to the L{root.Resolver}. """ stubResolver = StubResolver() deferredResolver = root.bootstrap(stubResolver) results = iter(stubResolver.pendingResults) d1 = next(results) for d in results: d.callback('192.0.2.101') d1.errback(TimeoutError()) def checkHints(res): self.assertEqual(deferredResolver.hints, ['192.0.2.101'] * 12) d1.addBoth(checkHints)
def test_injection(self): self.report['injected'] = None d = self.performALookup(self.input, self.resolver) @d.addCallback def cb(res): log.msg("The DNS query for %s is injected" % self.input) self.report['injected'] = True @d.addErrback def err(err): err.trap(defer.TimeoutError) log.msg("The DNS query for %s is not injected" % self.input) self.report['injected'] = False return d
def timeout(self): self.quietLoss = True self.transport.loseConnection() self.factory.noPage(defer.TimeoutError("Getting %s took longer than %s seconds." % (self.factory.url, self.factory.timeout)))
def testTimeoutTriggering(self): # Test that when the timeout does trigger, we get a defer.TimeoutError. return self.assertFailure( client.getPage(self.getURL("wait"), timeout=0.5), defer.TimeoutError)
def testTimeOut(self): """ Test that a Deferred which has setTimeout called on it and never has C{callback} or C{errback} called on it eventually fails with a L{error.TimeoutError}. """ L = [] d = defer.Deferred() d.setTimeout(0.01) self.assertFailure(d, defer.TimeoutError) d.addCallback(L.append) self.failIf(L, "Deferred failed too soon.") return d
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 test_setUp(self): self.failIf(detests.DeferredSetUpNeverFire.testCalled) result, suite = self._loadSuite(detests.DeferredSetUpNeverFire) suite(result) self.failIf(result.wasSuccessful()) self.failUnlessEqual(result.testsRun, 1) self.failUnlessEqual(len(result.failures), 0) self.failUnlessEqual(len(result.errors), 1) self.failIf(detests.DeferredSetUpNeverFire.testCalled) self.failUnless(result.errors[0][1].check(defer.TimeoutError))
def timeoutFactory(self): log.msg('timed out waiting for DTP connection') if self.deferred: d, self.deferred = self.deferred, None # TODO: LEFT OFF HERE! d.addErrback(debugDeferred, 'timeoutFactory firing errback') d.errback(defer.TimeoutError()) self.stopFactory()
def queryUDP(self, queries, timeout = None): """ Make a number of DNS queries via UDP. @type queries: A C{list} of C{dns.Query} instances @param queries: The queries to make. @type timeout: Sequence of C{int} @param timeout: Number of seconds after which to reissue the query. When the last timeout expires, the query is considered failed. @rtype: C{Deferred} @raise C{twisted.internet.defer.TimeoutError}: When the query times out. """ if timeout is None: timeout = self.timeout addresses = self.servers + list(self.dynServers) if not addresses: return defer.fail(IOError("No domain name servers available")) used = addresses.pop() return self.protocol.query(used, queries, timeout[0] ).addErrback(self._reissue, addresses, [used], queries, timeout )
def _reissue(self, reason, addressesLeft, addressesUsed, query, timeout): reason.trap(dns.DNSQueryTimeoutError) # If there are no servers left to be tried, adjust the timeout # to the next longest timeout period and move all the # "used" addresses back to the list of addresses to try. if not addressesLeft: addressesLeft = addressesUsed addressesLeft.reverse() addressesUsed = [] timeout = timeout[1:] # If all timeout values have been used, or the protocol has no # transport, this query has failed. Tell the protocol we're # giving up on it and return a terminal timeout failure to our # caller. if not timeout or self.protocol.transport is None: self.protocol.removeResend(reason.value.id) return failure.Failure(defer.TimeoutError(query)) # Get an address to try. Take it out of the list of addresses # to try and put it ino the list of already tried addresses. address = addressesLeft.pop() addressesUsed.append(address) # Issue a query to a server. Use the current timeout. Add this # function as a timeout errback in case another retry is required. d = self.protocol.query(address, query, timeout[0], reason.value.id) d.addErrback(self._reissue, addressesLeft, addressesUsed, query, timeout) return d
def _timeoutZone(self, d, controller, connector, seconds): connector.disconnect() controller.timeoutCall = None controller.deferred = None d.errback(error.TimeoutError("Zone lookup timed out after %d seconds" % (seconds,)))
def retry(t, p, *args): assert t, "Timeout is required" t = list(t) def errback(failure): failure.trap(defer.TimeoutError) if not t: return failure return p.query(timeout=t.pop(0), *args ).addErrback(errback ) return p.query(timeout=t.pop(0), *args ).addErrback(errback )
def __call__(self, failure): # AuthoritativeDomainErrors should halt resolution attempts failure.trap(dns.DomainError, defer.TimeoutError, NotImplementedError) return self.resolver(self.query, self.timeout)
def _on_discovery_timeout(self): if self._done: return self._done = True self.mcast.stopListening() self._discovery.errback(failure.Failure(defer.TimeoutError('in _on_discovery_timeout')))
def __call__(self, id): if id not in self.map: self.func(id) df = defer.Deferred() def timeout(): self.map[id].remove((df, timer)) if not self.map[id]: del self.map[id] df.errback(failure.Failure(defer.TimeoutError('in ReplyMatcher'))) timer = reactor.callLater(self.timeout, timeout) self.map.setdefault(id, set()).add((df, timer)) return df
def get_deferred(self, timeout=None): once = self.once df = defer.Deferred() id1 = once.watch(lambda *event: df.callback(event)) if timeout is not None: def do_timeout(): df.errback(failure.Failure(defer.TimeoutError('in Event.get_deferred'))) once.unwatch(id1) once.unwatch(x) delay = reactor.callLater(timeout, do_timeout) x = once.watch(lambda *event: delay.cancel()) return df