Python twisted.internet.task.deferLater() Examples
The following are 30
code examples of twisted.internet.task.deferLater().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
twisted.internet.task
, or try the search function
.
Example #1
Source File: test_detect.py From maas with GNU Affero General Public License v3.0 | 6 votes |
def test_deferredDHCPRequestErrback_cancels_all_on_FirstError(self): mock_cancelAll = self.patch(DHCPRequestMonitor, "cancelAll") def raise_ioerror(): raise IOError() a = deferLater(reactor, 0.0, raise_ioerror) b = deferLater(reactor, 6, lambda: "b") monitor = DHCPRequestMonitor("lo") monitor.deferredDHCPRequests = [a, b] deferredList = DeferredList( monitor.deferredDHCPRequests, consumeErrors=True, fireOnOneErrback=True, ) deferredList.addErrback(monitor.deferredDHCPRequestErrback) yield deferredList # Still have one call left in the reactor, since we mocked cancelAll(). b.cancel() self.assertThat(mock_cancelAll, MockCallsMatch(call([a, b])))
Example #2
Source File: worker.py From worker with GNU General Public License v3.0 | 6 votes |
def perform(self): try: trigger_id = yield self.db.getTriggerToCheck() while trigger_id is not None: acquired = yield self.db.setTriggerCheckLock(trigger_id) if acquired is not None: start = reactor.seconds() trigger = Trigger(trigger_id, self.db) yield trigger.check() end = reactor.seconds() yield self.db.delTriggerCheckLock(trigger_id) spy.TRIGGER_CHECK.report(end - start) trigger_id = yield self.db.getTriggerToCheck() yield task.deferLater(reactor, random.uniform(PERFORM_INTERVAL * 10, PERFORM_INTERVAL * 20), lambda: None) except GeneratorExit: pass except Exception as e: spy.TRIGGER_CHECK_ERRORS.report(0) log.error("Failed to perform triggers check: {e}", e=e) yield task.deferLater(reactor, ERROR_TIMEOUT, lambda: None)
Example #3
Source File: test_task.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cancel(self): """ The L{Deferred} returned by L{task.deferLater} can be cancelled to prevent the call from actually being performed. """ called = [] clock = task.Clock() d = task.deferLater(clock, 1, called.append, None) d.cancel() def cbCancelled(ignored): # Make sure there are no calls outstanding. self.assertEqual([], clock.getDelayedCalls()) # And make sure the call didn't somehow happen already. self.assertFalse(called) self.assertFailure(d, defer.CancelledError) d.addCallback(cbCancelled) return d
Example #4
Source File: loopback.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _loopbackAsyncContinue(ignored, server, serverToClient, client, clientToServer, pumpPolicy): # Clear the Deferred from each message queue, since it has already fired # and cannot be used again. clientToServer._notificationDeferred = None serverToClient._notificationDeferred = None # Schedule some more byte-pushing to happen. This isn't done # synchronously because no actual transport can re-enter dataReceived as # a result of calling write, and doing this synchronously could result # in that. from twisted.internet import reactor return deferLater( reactor, 0, _loopbackAsyncBody, server, serverToClient, client, clientToServer, pumpPolicy)
Example #5
Source File: tuntap.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def stopListening(self): """ Stop accepting connections on this port. This will shut down my socket and call self.connectionLost(). @return: A L{Deferred} that fires when this port has stopped. """ self.stopReading() if self.disconnecting: return self._stoppedDeferred elif self.connected: self._stoppedDeferred = task.deferLater( self.reactor, 0, self.connectionLost) self.disconnecting = True return self._stoppedDeferred else: return defer.succeed(None)
Example #6
Source File: loopback.py From learn_python3_spider with MIT License | 6 votes |
def _loopbackAsyncContinue(ignored, server, serverToClient, client, clientToServer, pumpPolicy): # Clear the Deferred from each message queue, since it has already fired # and cannot be used again. clientToServer._notificationDeferred = None serverToClient._notificationDeferred = None # Schedule some more byte-pushing to happen. This isn't done # synchronously because no actual transport can re-enter dataReceived as # a result of calling write, and doing this synchronously could result # in that. from twisted.internet import reactor return deferLater( reactor, 0, _loopbackAsyncBody, server, serverToClient, client, clientToServer, pumpPolicy)
Example #7
Source File: test_task.py From learn_python3_spider with MIT License | 6 votes |
def test_cancel(self): """ The L{Deferred} returned by L{task.deferLater} can be cancelled to prevent the call from actually being performed. """ called = [] clock = task.Clock() d = task.deferLater(clock, 1, called.append, None) d.cancel() def cbCancelled(ignored): # Make sure there are no calls outstanding. self.assertEqual([], clock.getDelayedCalls()) # And make sure the call didn't somehow happen already. self.assertFalse(called) self.assertFailure(d, defer.CancelledError) d.addCallback(cbCancelled) return d
Example #8
Source File: tuntap.py From learn_python3_spider with MIT License | 6 votes |
def stopListening(self): """ Stop accepting connections on this port. This will shut down my socket and call self.connectionLost(). @return: A L{Deferred} that fires when this port has stopped. """ self.stopReading() if self.disconnecting: return self._stoppedDeferred elif self.connected: self._stoppedDeferred = task.deferLater( self.reactor, 0, self.connectionLost) self.disconnecting = True return self._stoppedDeferred else: return defer.succeed(None)
Example #9
Source File: test_index_file.py From ccs-calendarserver with Apache License 2.0 | 6 votes |
def test_reserve_uid_timeout(self): # WARNING: This test is fundamentally flawed and will fail # intermittently because it uses the real clock. uid = "test-test-test" from twistedcaldav.config import config old_timeout = config.UIDReservationTimeOut config.UIDReservationTimeOut = 1 def _finally(): config.UIDReservationTimeOut = old_timeout d = self.db.isReservedUID(uid) d.addCallback(self.assertFalse) d.addCallback(lambda _: self.db.reserveUID(uid)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertTrue) d.addCallback(lambda _: deferLater(reactor, 2, lambda: None)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertFalse) self.addCleanup(_finally) return d
Example #10
Source File: test_index_file.py From ccs-calendarserver with Apache License 2.0 | 6 votes |
def test_reserve_uid_timeout(self): # WARNING: This test is fundamentally flawed and will fail # intermittently because it uses the real clock. uid = "test-test-test" from twistedcaldav.config import config old_timeout = config.UIDReservationTimeOut config.UIDReservationTimeOut = 1 def _finally(): config.UIDReservationTimeOut = old_timeout d = self.db.isReservedUID(uid) d.addCallback(self.assertFalse) d.addCallback(lambda _: self.db.reserveUID(uid)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertTrue) d.addCallback(lambda _: deferLater(reactor, 2, lambda: None)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertFalse) self.addCleanup(_finally) return d
Example #11
Source File: test_http.py From ccs-calendarserver with Apache License 2.0 | 6 votes |
def testTimeout_betweenRequests(self): cxn = self.connect(betweenRequestsTimeOut=0.3) cmds = [[]] data = "" cxn.client.write("GET / HTTP/1.1\r\n\r\n") cmds[0] += [('init', 'GET', '/', (1, 1), 0, ()), ('contentComplete',)] self.compareResult(cxn, cmds, data) response = TestResponse() response.headers.setRawHeaders("Content-Length", ("0",)) cxn.requests[0].writeResponse(response) response.finish() data += "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" self.compareResult(cxn, cmds, data) return deferLater(reactor, 0.5, self.assertDone, cxn) # Wait for timeout
Example #12
Source File: test_http.py From ccs-calendarserver with Apache License 2.0 | 6 votes |
def testTimeout_abortRequest(self): cxn = self.connect(allowPersistentConnections=False, closeTimeOut=0.3) cxn.client.transport.loseConnection = lambda: None cmds = [[]] data = "" cxn.client.write("GET / HTTP/1.1\r\n\r\n") cmds[0] += [('init', 'GET', '/', (1, 1), 0, ()), ('contentComplete',)] self.compareResult(cxn, cmds, data) response = TestResponse() response.headers.setRawHeaders("Content-Length", ("0",)) cxn.requests[0].writeResponse(response) response.finish() data += "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n" self.compareResult(cxn, cmds, data) def _check(cxn): self.assertDone(cxn) self.assertTrue(cxn.serverToClient.aborted) return deferLater(reactor, 0.5, self.assertDone, cxn) # Wait for timeout
Example #13
Source File: _rendezvous.py From magic-wormhole with MIT License | 6 votes |
def __attrs_post_init__(self): self._have_made_a_successful_connection = False self._stopping = False self._trace = None self._ws = None f = WSFactory(self, self._url) f.setProtocolOptions(autoPingInterval=60, autoPingTimeout=600) ep = self._make_endpoint(self._url) self._connector = internet.ClientService(ep, f) faf = None if self._have_made_a_successful_connection else 1 d = self._connector.whenConnected(failAfterFailures=faf) # if the initial connection fails, signal an error and shut down. do # this in a different reactor turn to avoid some hazards d.addBoth(lambda res: task.deferLater(self._reactor, 0.0, lambda: res)) # TODO: use EventualQueue d.addErrback(self._initial_connection_failed) self._debug_record_inbound_f = None
Example #14
Source File: beacon.py From iWant with MIT License | 6 votes |
def startProtocol(self): """ Join the multicast group and announce the identity and decide to become the leader if there is no response """ self.book.peers[self.book.uuidObj] = self._addr # datagrams can traverse more than one router hop self.transport.setTTL(5) self.transport.joinGroup(MCAST_ADDR[0]) self._broadcast_identity() self.log.msg('joining the multicast group and waiting for response') wait_for_response = 3 def response_from_peers(): self._eid = self.generate_election_id() self.log.msg('Announcing itself as the winner') self._leader(leader=self.book.uuidObj, eid=self._eid) self._npCallId = task.deferLater( reactor, wait_for_response, response_from_peers) self.d = threads.deferToThread(self._poll)
Example #15
Source File: db.py From worker with GNU General Public License v3.0 | 6 votes |
def acquireTriggerCheckLock(self, trigger_id, timeout): """ acquireTriggerCheckLock(self, trigger_id, timeout) Try to acquire lock for trigger check until timeout :param trigger_id: trigger identity :type trigger_id: string :param timeout: timeout in seconds :type timeout: float """ acquired = yield self.setTriggerCheckLock(trigger_id) count = 0 while acquired is None and count < timeout: count += 1 yield task.deferLater(reactor, 0.5, lambda: None) acquired = yield self.setTriggerCheckLock(trigger_id) if acquired is None: raise Exception("Can not acquire trigger lock in {0} seconds".format(timeout))
Example #16
Source File: test_detect.py From maas with GNU Affero General Public License v3.0 | 6 votes |
def test_cancelAll(self): self.errbacks_called = 0 def mock_errback(result: Failure): self.assertTrue(result.check(CancelledError)) self.errbacks_called += 1 a = deferLater(reactor, 6, lambda: "a") b = deferLater(reactor, 6, lambda: "b") a.addBoth(mock_errback) b.addBoth(mock_errback) deferreds = [a, b] DHCPRequestMonitor.cancelAll(deferreds) deferredList = DeferredList(deferreds) yield deferredList self.assertThat(self.errbacks_called, Equals(2))
Example #17
Source File: runtest.py From maas with GNU Affero General Public License v3.0 | 6 votes |
def _waitForThreadpoolToQuiesce(self, pool): """Return a :class:`Deferred` that waits for `pool` to quiesce.""" now = reactor.seconds() until = now + 90.0 while now < until: if self._isThreadpoolQuiet(pool): # The pool is quiet. It's safe to move on. Return the pool so # that it can still be reported. returnValue(pool) else: # Pause for a second to give it a chance to go quiet. now = yield deferLater(reactor, 1.0, reactor.seconds) else: # Despite waiting a long time the pool will not go quiet. The # validity of subsequent tests is compromised. Die immediately. print("ThreadPool", repr(pool), "is NOT quiet.", file=sys.stderr) os._exit(3)
Example #18
Source File: test_streamedlogs.py From gridsync with GNU General Public License v3.0 | 6 votes |
def test_reconnect_to_websocket(reactor, tahoe): """ If the connection to the WebSocket endpoint is lost, an attempt is made to re-establish. """ # This test might be simpler and more robust if it used # twisted.internet.task.Clock and twisted.test.proto_helpers.MemoryReactor # instead of a Mock reactor. from twisted.internet import reactor as real_reactor client_protocol = yield connect_to_log_endpoint( reactor, tahoe, real_reactor, FakeLogServerProtocol ) client_protocol.transport.abortConnection() # Let the reactor process the disconnect. yield deferLater(real_reactor, 0.0, lambda: None) advance_mock_clock(reactor) assert reactor.connectTCP.call_count == 2 host, port, _ = reactor.connectTCP.call_args[0] expected_url = urlsplit(tahoe.nodeurl) assert "{}:{}".format(host, port) == expected_url.netloc
Example #19
Source File: test_streamedlogs.py From gridsync with GNU General Public License v3.0 | 6 votes |
def test_collect_eliot_logs(reactor, tahoe): """ ``StreamedLogs`` saves the JSON log messages so that they can be retrieved using ``Tahoe.get_streamed_log_messages``. """ from twisted.internet import reactor as real_reactor yield connect_to_log_endpoint( reactor, tahoe, real_reactor, FakeLogServerProtocol ) # Arbitrarily give it about a second to deliver the message. All the I/O # is loopback and the data is small. One second should be plenty of time. for i in range(20): messages = tahoe.get_streamed_log_messages() if FakeLogServerProtocol.FAKE_MESSAGE in messages: break yield deferLater(real_reactor, 0.1, lambda: None) messages = tahoe.get_streamed_log_messages() assert FakeLogServerProtocol.FAKE_MESSAGE in messages
Example #20
Source File: tahoe.py From gridsync with GNU General Public License v3.0 | 6 votes |
def restart(self): from twisted.internet import reactor log.debug("Restarting %s client...", self.name) if self.state in (Tahoe.STOPPING, Tahoe.STARTING): log.warning( "Aborting restart operation; " 'the "%s" client is already (re)starting', self.name, ) return # Temporarily disable desktop notifications for (dis)connect events pref = get_preference("notifications", "connection") set_preference("notifications", "connection", "false") yield self.stop() if sys.platform == "win32": yield deferLater(reactor, 0.1, lambda: None) self._win32_cleanup() yield self.start() yield self.await_ready() yield deferLater(reactor, 1, lambda: None) set_preference("notifications", "connection", pref) log.debug("Finished restarting %s client.", self.name)
Example #21
Source File: client_protocol.py From joinmarket-clientserver with GNU General Public License v3.0 | 6 votes |
def unconfirm_callback(self, txd, txid): #find the offer for this tx offerinfo = self.tx_match(txd) if not offerinfo: return False to_cancel, to_announce = self.client.on_tx_unconfirmed(offerinfo, txid) self.client.modify_orders(to_cancel, to_announce) txinfo = tuple((x["script"], x["value"]) for x in txd["outs"]) confirm_timeout_sec = float(jm_single().config.get( "TIMEOUT", "confirm_timeout_hours")) * 3600 task.deferLater(reactor, confirm_timeout_sec, self.client.wallet_service.check_callback_called, txinfo, self.confirm_callback, "confirmed", "transaction with outputs " + str(txinfo) + " not confirmed.") d = self.callRemote(commands.JMAnnounceOffers, to_announce=json.dumps(to_announce), to_cancel=json.dumps(to_cancel), offerlist=json.dumps(self.client.offerlist)) self.defaultCallbacks(d) return True
Example #22
Source File: test_task.py From python-for-android with Apache License 2.0 | 6 votes |
def test_callback(self): """ The L{Deferred} returned by L{task.deferLater} is called back after the specified delay with the result of the function passed in. """ results = [] flag = object() def callable(foo, bar): results.append((foo, bar)) return flag clock = task.Clock() d = task.deferLater(clock, 3, callable, 'foo', bar='bar') d.addCallback(self.assertIdentical, flag) clock.advance(2) self.assertEqual(results, []) clock.advance(1) self.assertEqual(results, [('foo', 'bar')]) return d
Example #23
Source File: loopback.py From python-for-android with Apache License 2.0 | 6 votes |
def _loopbackAsyncContinue(ignored, server, serverToClient, client, clientToServer, pumpPolicy): # Clear the Deferred from each message queue, since it has already fired # and cannot be used again. clientToServer._notificationDeferred = None serverToClient._notificationDeferred = None # Schedule some more byte-pushing to happen. This isn't done # synchronously because no actual transport can re-enter dataReceived as # a result of calling write, and doing this synchronously could result # in that. from twisted.internet import reactor return deferLater( reactor, 0, _loopbackAsyncBody, server, serverToClient, client, clientToServer, pumpPolicy)
Example #24
Source File: beacon.py From iWant with MIT License | 6 votes |
def _broadcast_re_election(self): def election_timeout_callback(): eid = self.generate_election_id() self.log.msg('broadcast re-election id {0}'.format(eid)) msg = bake(RE_ELECTION, election_id=eid) self.send(msg, MCAST_ADDR) if self._reelectionClock is None: self._reelectionClock = reactor election_timeout = self.generate_timeout() self.log.msg('{0} timeout: ECommencement msg'.format(election_timeout)) # print '{0} timeout: ECommencement msg'.format(election_timeout) # broadcast relection can be called twice if leader announces its death # and polling also determines leaders death self.cancel_election_commencement_callback() self._reelectionCallId = task.deferLater( reactor, election_timeout, election_timeout_callback)
Example #25
Source File: vmware_exporter.py From vmware_exporter with BSD 3-Clause "New" or "Revised" License | 6 votes |
def render_GET(self, request): """ handles get requests for metrics, health, and everything else """ path = request.path.decode() request.setHeader("Content-Type", "text/plain; charset=UTF-8") if path == '/metrics': deferred_request = deferLater(reactor, 0, lambda: request) deferred_request.addCallback(self.generate_latest_metrics) deferred_request.addErrback(self.errback, request) return NOT_DONE_YET elif path == '/healthz': request.setResponseCode(200) log("Service is UP") return 'Server is UP'.encode() else: log("Uri not found: " + request.uri) request.setResponseCode(404) return '404 Not Found'.encode()
Example #26
Source File: wallet_service.py From joinmarket-clientserver with GNU General Public License v3.0 | 5 votes |
def request_sync_wallet(self): """ Ensures wallet sync is complete before the main event loop starts. """ if self.bci is not None: d = task.deferLater(reactor, 0.0, self.sync_wallet) d.addCallback(self.start_wallet_monitoring)
Example #27
Source File: taker.py From joinmarket-clientserver with GNU General Public License v3.0 | 5 votes |
def unconfirm_callback(self, txd, txid): if not self.tx_match(txd): return False jlog.info("Transaction seen on network, waiting for confirmation") #To allow client to mark transaction as "done" (e.g. by persisting state) self.on_finished_callback(True, fromtx="unconfirmed") self.waiting_for_conf = True confirm_timeout_sec = float(jm_single().config.get( "TIMEOUT", "confirm_timeout_hours")) * 3600 task.deferLater(reactor, confirm_timeout_sec, self.wallet_service.check_callback_called, txid, self.confirm_callback, "confirmed", "transaction with txid " + str(txid) + " not confirmed.") return True
Example #28
Source File: client_protocol.py From joinmarket-clientserver with GNU General Public License v3.0 | 5 votes |
def on_JM_TX_RECEIVED(self, nick, txhex, offer): # "none" flags p2ep protocol; pass through to the generic # on_tx handler for that: if offer == "none": return self.on_p2ep_tx_received(nick, txhex) offer = json.loads(offer) retval = self.client.on_tx_received(nick, txhex, offer) if not retval[0]: jlog.info("Maker refuses to continue on receipt of tx") else: sigs = retval[1] self.finalized_offers[nick] = offer tx = btc.deserialize(txhex) self.finalized_offers[nick]["txd"] = tx txid = btc.txhash(btc.serialize(tx)) # we index the callback by the out-set of the transaction, # because the txid is not known until all scriptSigs collected # (hence this is required for Makers, but not Takers). # For more info see WalletService.transaction_monitor(): txinfo = tuple((x["script"], x["value"]) for x in tx["outs"]) self.client.wallet_service.register_callbacks([self.unconfirm_callback], txinfo, "unconfirmed") self.client.wallet_service.register_callbacks([self.confirm_callback], txinfo, "confirmed") task.deferLater(reactor, float(jm_single().config.getint("TIMEOUT", "unconfirm_timeout_sec")), self.client.wallet_service.check_callback_called, txinfo, self.unconfirm_callback, "unconfirmed", "transaction with outputs: " + str(txinfo) + " not broadcast.") d = self.callRemote(commands.JMTXSigs, nick=nick, sigs=json.dumps(sigs)) self.defaultCallbacks(d) return {"accepted": True}
Example #29
Source File: test_task.py From python-for-android with Apache License 2.0 | 5 votes |
def test_errback(self): """ The L{Deferred} returned by L{task.deferLater} is errbacked if the supplied function raises an exception. """ def callable(): raise TestException() clock = task.Clock() d = task.deferLater(clock, 1, callable) clock.advance(1) return self.assertFailure(d, TestException)
Example #30
Source File: testutil.py From afkak with Apache License 2.0 | 5 votes |
def async_delay(timeout=0.01, clock=None): if clock is None: from twisted.internet import reactor as clock return task.deferLater(clock, timeout, lambda: None)