Python twisted.internet.reactor.callLater() Examples
The following are 30
code examples of twisted.internet.reactor.callLater().
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.reactor
, or try the search function
.
Example #1
Source File: test_twisted.py From python-consul2 with MIT License | 6 votes |
def test_catalog(self, consul_port): c = consul.twisted.Consul(port=consul_port) @defer.inlineCallbacks def register(): response = yield c.catalog.register('n1', '10.1.10.11') assert response is True yield sleep(50 / 1000.0) response = yield c.catalog.deregister('n1') assert response is True reactor.callLater(1.0 / 100, register) index, nodes = yield c.catalog.nodes() assert len(nodes) == 1 current = nodes[0] index, nodes = yield c.catalog.nodes(index=index) nodes.remove(current) assert [x['Node'] for x in nodes] == ['n1'] index, nodes = yield c.catalog.nodes(index=index) nodes.remove(current) assert [x['Node'] for x in nodes] == []
Example #2
Source File: client.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _putConnection(self, key, connection): """ Return a persistent connection to the pool. This will be called by L{HTTP11ClientProtocol} when the connection becomes quiescent. """ if connection.state != "QUIESCENT": # Log with traceback for debugging purposes: try: raise RuntimeError( "BUG: Non-quiescent protocol added to connection pool.") except: log.err() return connections = self._connections.setdefault(key, []) if len(connections) == self.maxPersistentPerHost: dropped = connections.pop(0) dropped.transport.loseConnection() self._timeouts[dropped].cancel() del self._timeouts[dropped] connections.append(connection) cid = self._reactor.callLater(self.cachedConnectionTimeout, self._removeConnection, key, connection) self._timeouts[connection] = cid
Example #3
Source File: test_http2.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_lateCompletionWorks(self): """ L{H2Connection} correctly unblocks when a stream is ended. """ connection = H2Connection() connection.requestFactory = DelayedHTTPHandler _, transport = self.connectAndReceive( connection, self.getRequestHeaders, [] ) # Delay a call to end request, forcing the connection to block because # it has no data to send. request = connection.streams[1]._request reactor.callLater(0.01, request.finish) def validateComplete(*args): frames = framesFromBytes(transport.value()) # Check that the stream is correctly terminated. self.assertEqual(len(frames), 3) self.assertTrue('END_STREAM' in frames[-1].flags) return connection._streamCleanupCallbacks[1].addCallback( validateComplete )
Example #4
Source File: process.py From bitmask-dev with GNU General Public License v3.0 | 6 votes |
def _connect_to_management(self, retries=30): if retries == 0: self.log.error('Timeout while connecting to management') self.failed = True return def retry(retries): ctr = retries - 1 self.log.warn( 'Error connecting to management, retrying. ' 'Retries left: %s' % ctr) reactor.callLater( 0.1, self._connect_to_management, ctr) self._d = connectProtocol( self._management_endpoint, ManagementProtocol(verbose=True)) self._d.addCallbacks( self._got_management_protocol, lambda f: retry(retries))
Example #5
Source File: test_gateway.py From bitmask-dev with GNU General Public License v3.0 | 6 votes |
def getReply(self, line, proto, transport): proto.lineReceived(line) if line[:4] not in ['HELO', 'MAIL', 'RCPT', 'DATA']: return succeed("") def check_transport(_): reply = transport.value() if reply: transport.clear() return succeed(reply) d = Deferred() d.addCallback(check_transport) reactor.callLater(0, lambda: d.callback(None)) return d return check_transport(None)
Example #6
Source File: Sensei.py From Timeline with GNU General Public License v3.0 | 6 votes |
def play(self, client, param, tab = -1): if param[0] == 'ir' and (self.tabMatch is None or not self.tabMatch.battleStarted): moveSpins = ','.join(map(str, self.moveSpins)) for p in self: pCards = '' if p in self.Playing: pCards = ','.join(map(str, map(int, self.Playing[self.Playing.index(p)].deck))) p.send('zm', 'nt', self.slotPlayer.index, moveSpins, pCards) self.boardTimeoutHandler = reactor.callLater(22, self.checkGameStatus) else: gameStatus = super(CardJitsuFireSenseiGame, self).play(client, param, tab) if param[0] == 'ir' and gameStatus: if self.tabPlayer == self.Playing[0]: # sensei self.checkGameStatus() if param[0] == 'cc' and gameStatus: self.checkBattleStatus(True)
Example #7
Source File: test_gireactor.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_cantRegisterAfterRun(self): """ It is not possible to register a C{Application} after the reactor has already started. """ reactor = gireactor.GIReactor(useGtk=False) self.addCleanup(self.unbuildReactor, reactor) app = Gio.Application( application_id='com.twistedmatrix.trial.gireactor', flags=Gio.ApplicationFlags.FLAGS_NONE) def tryRegister(): exc = self.assertRaises(ReactorAlreadyRunning, reactor.registerGApplication, app) self.assertEqual(exc.args[0], "Can't register application after reactor was started.") reactor.stop() reactor.callLater(0, tryRegister) ReactorBuilder.runReactor(self, reactor)
Example #8
Source File: graphite.py From worker with GNU General Public License v3.0 | 6 votes |
def connect(self, reconnecting=False): if self.connecting and not reconnecting: return self.connecting = True end_point = TCP4ClientEndpoint(reactor, self.host, self.port, 10) d = end_point.connect(Factory.forProtocol(GraphiteProtocol)) def success(connection): self.connecting = False log.info('Connected to {replica}', replica=self) self.connection = connection def failed(error): log.error('Connect to {replica} failed: {error}', replica=self, error=error) reactor.callLater(10, self.connect, True) d.addCallbacks(success, failed)
Example #9
Source File: bashplex.py From epoptes with GNU General Public License v3.0 | 6 votes |
def connectionMade(self): """Override BaseProtocol.connectionMade.""" def forward_connection(_result): """Callback for startup_commands.""" exchange.client_connected(self.handle, self) # noinspection PyUnresolvedReferences self.ping_timer = reactor.callLater( self.factory.ping_interval, self.ping) def kill_connection(error): """Errback for startup_commands.""" LOG.e("Error: Could not send the startup functions to the client:", error) self._loseConnection() peer = self.transport.getPeer() self.handle = "{}:{}".format(peer.host, peer.port) LOG.w("Connected:", self.handle) dfr = self.command(self.factory.startup_commands) dfr.addCallback(forward_connection) dfr.addErrback(kill_connection)
Example #10
Source File: test_inotify.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_noAutoAddSubdirectory(self): """ L{inotify.INotify.watch} with autoAdd==False will stop inotify from watching subdirectories created under the watched one. """ def _callback(wp, fp, mask): # We are notified before we actually process new # directories, so we need to defer this check. def _(): try: self.assertFalse(self.inotify._isWatched(subdir)) d.callback(None) except Exception: d.errback() reactor.callLater(0, _) checkMask = inotify.IN_ISDIR | inotify.IN_CREATE self.inotify.watch( self.dirname, mask=checkMask, autoAdd=False, callbacks=[_callback]) subdir = self.dirname.child('test') d = defer.Deferred() subdir.createDirectory() return d
Example #11
Source File: nodeservice.py From kotori with GNU Affero General Public License v3.0 | 6 votes |
def onSessionOpen(self): self.prefix("broadcast", "de.elmyra.kotori.broadcast") self.prefix("presence", "http://kotori.elmyra.de/presence?") self.prefix("node", "http://kotori.elmyra.de/node/{0}#".format(NODE_ID)) self.subscribe("broadcast:node-heartbeat", self.dump_event) self.subscribe("presence:node_id={0}&hostname={1}".format(NODE_ID, NODE_HOSTNAME), lambda: x) self.subscribe("node:say", self.dump_event) self.subscribe("node:say", self.say) print "INFO: -> Node successfully connected to master" self.heartbeat() reactor.callLater(0, node_manager.start_features, self)
Example #12
Source File: nexus.py From Paradrop with Apache License 2.0 | 6 votes |
def __init__(self, stealStdio=True, printToConsole=True): ''' The one big thing this function leaves out is reactor.start(). Call this externally *after * initializing a nexus object. ''' self.session = None self.wamp_connected = False self.jwt_valid = False self.info = AttrWrapper() resolveInfo(self, settings.CONFIG_FILE) self.info.setOnChange(self.onInfoChange) # initialize output. If filepath is set, logs to file. # If stealStdio is set intercepts all stderr and stdout and interprets it internally # If printToConsole is set (defaults True) all final output is rendered to stdout output.out.startLogging(filePath=settings.LOG_DIR, stealStdio=stealStdio, printToConsole=printToConsole) # register onStop for the shutdown call reactor.addSystemEventTrigger('before', 'shutdown', self.onStop) # The reactor needs to be runnnig before this call is fired, since we start the session # here. Assuming callLater doesn't fire until thats happened reactor.callLater(0, self.onStart)
Example #13
Source File: network.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def circuitCompleted(self, conn_to_flush): """ Circuit was just completed; that is, its endpoints are now connected. Do all the things we have to do now. """ if self.closed: log.debug("%s: Completed circuit while closed. Ignoring.", self.name) return log.debug("%s: Circuit completed." % self.name) # Set us as the circuit of our pluggable transport instance. self.transport.circuit = self # Call the transport-specific circuitConnected method since # this is a good time to perform a handshake. self.transport.circuitConnected() # Do a dummy dataReceived on the initiating connection in case # it has any buffered data that must be flushed to the network. # # (We use callLater because we want to return back to the # event loop so that any messages we send in circuitConnected get sent # to the network immediately.) reactor.callLater(0.01, conn_to_flush.dataReceived, '')
Example #14
Source File: nodeservice.py From kotori with GNU Affero General Public License v3.0 | 6 votes |
def boot_node(websocket_uri, debug=False, trace=False): print 'INFO: Starting node service, connecting to', websocket_uri # connect to master service """ global node_manager node_manager = NodeManager(websocket_uri, debug=debug) reactor.callLater(0, node_manager.master_connect) """ runner = ApplicationRunner(websocket_uri, u'kotori-realm', debug=trace, debug_wamp=debug, debug_app=debug) runner.run(KotoriNode, start_reactor=False) #app = Application() #app.run(url=websocket_uri, realm='kotori-realm', debug=True, debug_wamp=True, debug_app=True, start_reactor=False)
Example #15
Source File: scramblesuit.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def flushPieces( self ): """ Write the application data in chunks to the wire. The cached data is sent over the wire in chunks. After every write call, control is given back to the Twisted reactor so it has a chance to flush the data. Shortly thereafter, this function is called again to write the next chunk of data. The delays in between subsequent write calls are controlled by the inter-arrival time obfuscator. """ # Drain and send an MTU-sized chunk from the chopping buffer. if len(self.choppingBuf) > const.MTU: self.circuit.downstream.write(self.choppingBuf.read(const.MTU)) # Drain and send whatever is left in the output buffer. else: blurb = self.choppingBuf.read() padBlurb = self.pktMorpher.getPadding(self.sendCrypter, self.sendHMAC, len(blurb)) self.circuit.downstream.write(blurb + padBlurb) return reactor.callLater(self.iatMorpher.randomSample(), self.flushPieces)
Example #16
Source File: client.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def buildProtocol(self, addr): p = protocol.ClientFactory.buildProtocol(self, addr) p.followRedirect = self.followRedirect p.afterFoundGet = self.afterFoundGet if self.timeout: timeoutCall = reactor.callLater(self.timeout, p.timeout) self.deferred.addBoth(self._cancelTimeout, timeoutCall) return p
Example #17
Source File: spawn_process.py From epoptes with GNU General Public License v3.0 | 5 votes |
def main(): """Run a test from the command line.""" def on_test_exit(out_data, err_data, reason): """Called by SpawnProcess when the test process exits.""" LOG.d(" Callback out_data:", out_data) LOG.d(" Callback err_data:", err_data) LOG.d(" Callback reason:", reason) # Quit a bit later in order to test if events occur properly # noinspection PyUnresolvedReferences reactor.callLater(3, reactor.stop) script = """# Produce some output with delays i=0 while [ $i -lt 5 ]; do echo "process stdout line #$i" echo "process stderr line #$i" >&2 sleep 1 i=$(($i+1)) done """ tests = ['process-error'] print("Running tests:", tests) if 'normal-termination' in tests: SpawnProcess(on_test_exit).spawn(['sh', '-c', script]) if 'lines_max' in tests: SpawnProcess(on_test_exit).spawn( ['sh', '-c', 'trap "" TERM\n' + script], lines_max=3) if 'timeout' in tests: SpawnProcess(on_test_exit).spawn(['sh', '-c', script], timeout=3) if 'event-driven' in tests: # e.g. a cancel button proc = SpawnProcess(on_test_exit) proc.spawn(['sh', '-c', script]) # noinspection PyUnresolvedReferences reactor.callLater(3, proc.stop, 'cancelled') if 'process-error' in tests: SpawnProcess(on_test_exit).spawn( ['sh', '-c', 'echo Started && sleep 1 && invalid-command']) # noinspection PyUnresolvedReferences reactor.run()
Example #18
Source File: test_gireactor.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def runReactor(self, app, reactor): """ Register the app, run the reactor, make sure app was activated, and that reactor was running, and that reactor can be stopped. """ if not hasattr(app, "quit"): raise SkipTest("Version of PyGObject is too old.") result = [] def stop(): result.append("stopped") reactor.stop() def activate(widget): result.append("activated") reactor.callLater(0, stop) app.connect('activate', activate) # We want reactor.stop() to *always* stop the event loop, even if # someone has called hold() on the application and never done the # corresponding release() -- for more details see # http://developer.gnome.org/gio/unstable/GApplication.html. app.hold() reactor.registerGApplication(app) ReactorBuilder.runReactor(self, reactor) self.assertEqual(result, ["activated", "stopped"])
Example #19
Source File: test_posixbase.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_delayedCall(self): """ If there is a delayed call, C{doIteration} is called with a timeout which is the difference between the current time and the time at which that call is to run. """ reactor = TimeoutReportReactor() reactor.callLater(100, lambda: None) timeout = self._checkIterationTimeout(reactor) self.assertEqual(timeout, 100)
Example #20
Source File: test_inotify.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_simpleDeleteDirectory(self): """ L{inotify.INotify} removes a directory from the watchlist when it's removed from the filesystem. """ calls = [] def _callback(wp, filename, mask): # We are notified before we actually process new # directories, so we need to defer this check. def _(): try: self.assertTrue(self.inotify._isWatched(subdir)) subdir.remove() except Exception: d.errback() def _eb(): # second call, we have just removed the subdir try: self.assertFalse(self.inotify._isWatched(subdir)) d.callback(None) except Exception: d.errback() if not calls: # first call, it's the create subdir calls.append(filename) reactor.callLater(0, _) else: reactor.callLater(0, _eb) checkMask = inotify.IN_ISDIR | inotify.IN_CREATE self.inotify.watch( self.dirname, mask=checkMask, autoAdd=True, callbacks=[_callback]) subdir = self.dirname.child('test') d = defer.Deferred() subdir.createDirectory() return d
Example #21
Source File: test_posixbase.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_timePasses(self): """ If a delayed call is scheduled and then some time passes, the timeout passed to C{doIteration} is reduced by the amount of time which passed. """ reactor = TimeoutReportReactor() reactor.callLater(100, lambda: None) reactor.now += 25 timeout = self._checkIterationTimeout(reactor) self.assertEqual(timeout, 75)
Example #22
Source File: scramblesuit.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def sendRemote( self, data, flags=const.FLAG_PAYLOAD ): """ Send data to the remote end after a connection was established. The given `data' is first encapsulated in protocol messages. Then, the protocol message(s) are sent over the wire. The argument `flags' specifies the protocol message flags with the default flags signalling payload. """ log.debug("Processing %d bytes of outgoing data." % len(data)) # Wrap the application's data in ScrambleSuit protocol messages. messages = message.createProtocolMessages(data, flags=flags) blurb = "".join([msg.encryptAndHMAC(self.sendCrypter, self.sendHMAC) for msg in messages]) # Flush data chunk for chunk to obfuscate inter-arrival times. if const.USE_IAT_OBFUSCATION: if len(self.choppingBuf) == 0: self.choppingBuf.write(blurb) reactor.callLater(self.iatMorpher.randomSample(), self.flushPieces) else: # flushPieces() is still busy processing the chopping buffer. self.choppingBuf.write(blurb) else: padBlurb = self.pktMorpher.getPadding(self.sendCrypter, self.sendHMAC, len(blurb)) self.circuit.downstream.write(blurb + padBlurb)
Example #23
Source File: tcpserver.py From txTrader with MIT License | 5 votes |
def cmd_shutdown(self, line): self.factory.output('client at %s requested shutdown' % self.transport.getPeer()) self.factory.api.close_client(self) reactor.callLater(1, reactor.stop)
Example #24
Source File: webserver.py From txTrader with MIT License | 5 votes |
def json_shutdown(self, args, d): """shutdown(message) Request server shutdown """ message = str(args['message']) self.output('ALERT: shutdown: %s' % message) # self.output('shutdown()') reactor.callLater(1, reactor.stop) self.render(d, 'shutdown requested')
Example #25
Source File: test_webclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def render(self, request): def response(): request.write(b'some bytes') request.finish() reactor.callLater(self.seconds, response) return server.NOT_DONE_YET
Example #26
Source File: spawn_process.py From epoptes with GNU General Public License v3.0 | 5 votes |
def call_on_exit(self): """Call on_exit after processing all twisted events. When e.g. the iperf port is already in use, processExited happens before errReceived gets "bind failed: Address already in use". Using callLater, on_exit is able to properly get and display stderr. """ self.on_exit(self.out_data, self.err_data, self.reason)
Example #27
Source File: spawn_process.py From epoptes with GNU General Public License v3.0 | 5 votes |
def stop(self, reason=""): """Send TERM, TERM, KILL, with 0.5 sec delays.""" LOG.d("stop('{}')".format(reason)) assert self.state != "idle" assert self.transport # Once stopping started, only accept calls from stop itself, # and not for example lines_max calls. if self.state.startswith("stopping") \ and not reason.startswith("stopping"): return # Only keep the initial reason if not self.reason: self.reason = reason # Cancel the timeout callback if self.dc_timeout: if self.reason != "timeout": self.dc_timeout.cancel() self.dc_timeout = None if self.state == "running": self.state = "stopping1" self.transport.signalProcess('TERM') elif self.state == "stopping1": self.state = "stopping2" # iperf2 requests 2 TERM signals to abort waiting open connections # Additionally, help it by closing its stdio self.transport.loseConnection() self.transport.signalProcess('TERM') elif self.state == "stopping2": self.state = "stopping3" # Kill it, don't wait any longer self.transport.signalProcess('KILL') else: assert False, "Unable to kill child process!" # noinspection PyUnresolvedReferences self.dc_stop = reactor.callLater(0.5, self.stop, self.state)
Example #28
Source File: spawn_process.py From epoptes with GNU General Public License v3.0 | 5 votes |
def spawn(self, cmdline, timeout=0, lines_max=0): """Spawn the process and run on_exit on exit, timeout, or lines_max.""" LOG.d("spawn: {}, timeout: {}, lines_max: {}".format( cmdline, timeout, lines_max)) assert self.state == "idle" # Reinitialize vars without duplicating a lot of code self.__init__(self.on_exit) self.lines_max = lines_max if timeout: # noinspection PyUnresolvedReferences self.dc_timeout = reactor.callLater(timeout, self.stop, "timeout") else: self.dc_timeout = None # noinspection PyUnresolvedReferences reactor.spawnProcess(self, cmdline[0], cmdline)
Example #29
Source File: bashplex.py From epoptes with GNU General Public License v3.0 | 5 votes |
def ping_response(self, _): """Receive the responses from the client pings.""" # Responses that arrive after a client has timed out mean a "reconnect" if self.timed_out: LOG.w("Reconnected:", self.handle) exchange.client_reconnected(self.handle) self.timed_out = False else: self.ping_timeout.cancel() # noinspection PyUnresolvedReferences self.ping_timer = reactor.callLater( self.factory.ping_interval, self.ping)
Example #30
Source File: bashplex.py From epoptes with GNU General Public License v3.0 | 5 votes |
def ping(self): """Periodically ping the clients.""" self.command('ping').addCallback(self.ping_response) # noinspection PyUnresolvedReferences self.ping_timeout = reactor.callLater( self.factory.ping_timeout, self.ping_timed_out)