我们从Python开源项目中,提取了以下45个代码示例,用于说明如何使用twisted.internet.reactor.fireSystemEvent()。
def __init__(self, io_loop=None): if not io_loop: io_loop = tornado.ioloop.IOLoop.current() self._io_loop = io_loop self._readers = {} # map of reader objects to fd self._writers = {} # map of writer objects to fd self._fds = {} # a map of fd to a (reader, writer) tuple self._delayedCalls = {} PosixReactorBase.__init__(self) self.addSystemEventTrigger('during', 'shutdown', self.crash) # IOLoop.start() bypasses some of the reactor initialization. # Fire off the necessary events if they weren't already triggered # by reactor.run(). def start_if_necessary(): if not self._started: self.fireSystemEvent('startup') self._io_loop.add_callback(start_if_necessary) # IReactorTime
def start_server_and_listen(use_auth, analytics_manager, max_tries=5): """The primary entry point for launching the daemon. Args: use_auth: set to true to enable http authentication analytics_manager: to send analytics """ analytics_manager.send_server_startup() daemon_server = DaemonServer(analytics_manager) try: yield daemon_server.start(use_auth) analytics_manager.send_server_startup_success() except Exception as e: log.exception('Failed to startup') yield daemon_server.stop() analytics_manager.send_server_startup_error(str(e)) reactor.fireSystemEvent("shutdown")
def test_wsgi(self): """ The I{--wsgi} option takes the fully-qualifed Python name of a WSGI application object and creates a L{WSGIResource} at the root which serves that application. """ options = Options() options.parseOptions(['--wsgi', __name__ + '.application']) root = options['root'] self.assertTrue(root, WSGIResource) self.assertIdentical(root._reactor, reactor) self.assertTrue(isinstance(root._threadpool, ThreadPool)) self.assertIdentical(root._application, application) # The threadpool should start and stop with the reactor. self.assertFalse(root._threadpool.started) reactor.fireSystemEvent('startup') self.assertTrue(root._threadpool.started) self.assertFalse(root._threadpool.joined) reactor.fireSystemEvent('shutdown') self.assertTrue(root._threadpool.joined)
def test_beforePreceedsDuring(self): """ L{IReactorCore.addSystemEventTrigger} should call triggers added to the C{'before'} phase before it calls triggers added to the C{'during'} phase. """ eventType = 'test' events = [] def beforeTrigger(): events.append('before') def duringTrigger(): events.append('during') self.addTrigger('before', eventType, beforeTrigger) self.addTrigger('during', eventType, duringTrigger) self.assertEqual(events, []) reactor.fireSystemEvent(eventType) self.assertEqual(events, ['before', 'during'])
def test_duringPreceedsAfter(self): """ L{IReactorCore.addSystemEventTrigger} should call triggers added to the C{'during'} phase before it calls triggers added to the C{'after'} phase. """ eventType = 'test' events = [] def duringTrigger(): events.append('during') def afterTrigger(): events.append('after') self.addTrigger('during', eventType, duringTrigger) self.addTrigger('after', eventType, afterTrigger) self.assertEqual(events, []) reactor.fireSystemEvent(eventType) self.assertEqual(events, ['during', 'after'])
def test_beforeReturnsDeferred(self): """ If a trigger added to the C{'before'} phase of an event returns a L{Deferred}, the C{'during'} phase should be delayed until it is called back. """ triggerDeferred = Deferred() eventType = 'test' events = [] def beforeTrigger(): return triggerDeferred def duringTrigger(): events.append('during') self.addTrigger('before', eventType, beforeTrigger) self.addTrigger('during', eventType, duringTrigger) self.assertEqual(events, []) reactor.fireSystemEvent(eventType) self.assertEqual(events, []) triggerDeferred.callback(None) self.assertEqual(events, ['during'])
def test_removeSystemEventTrigger(self): """ A trigger removed with L{IReactorCore.removeSystemEventTrigger} should not be called when the event fires. """ eventType = 'test' events = [] def firstBeforeTrigger(): events.append('first') def secondBeforeTrigger(): events.append('second') self.addTrigger('before', eventType, firstBeforeTrigger) self.removeTrigger( self.addTrigger('before', eventType, secondBeforeTrigger)) self.assertEqual(events, []) reactor.fireSystemEvent(eventType) self.assertEqual(events, ['first'])
def stop(self): PosixReactorBase.stop(self) fire_shutdown = functools.partial(self.fireSystemEvent, "shutdown") self._io_loop.add_callback(fire_shutdown)
def _bail(self): from twisted.internet import reactor d = defer.Deferred() reactor.addSystemEventTrigger('after', 'shutdown', lambda: d.callback(None)) reactor.fireSystemEvent('shutdown') # radix's suggestion treactor = interfaces.IReactorThreads(reactor, None) if treactor is not None: treactor.suggestThreadPoolSize(0) # As long as TestCase does crap stuff with the reactor we need to # manually shutdown the reactor here, and that requires util.wait # :( # so that the shutdown event completes unittest.TestCase('mktemp')._wait(d)
def connect_factory(host, port, factory, blob_storage, hash_to_process): from twisted.internet import reactor @defer.inlineCallbacks def on_finish(result): log.info("Finished sending %s", hash_to_process) yield update_sent_blobs(factory.p.blob_hashes_sent, host, blob_storage) connection.disconnect() reactor.fireSystemEvent("shutdown") @defer.inlineCallbacks def on_error(error): log.error("Error when sending %s: %s. Hashes sent %s", hash_to_process, error, factory.p.blob_hashes_sent) yield update_sent_blobs(factory.p.blob_hashes_sent, host, blob_storage) connection.disconnect() reactor.fireSystemEvent("shutdown") def on_connection_fail(result): log.error("Failed to connect to %s:%s", host, port) reactor.fireSystemEvent("shutdown") def _error(failure): log.error("Failed on_connection_lost_d callback: %s", failure) reactor.fireSystemEvent("shutdown") factory.on_connection_lost_d.addCallbacks(on_finish, on_error) factory.on_connection_lost_d.addErrback(_error) factory.on_connection_fail_d.addCallback(on_connection_fail) try: log.debug("Connecting factory to %s:%s", host, port) connection = reactor.connectTCP(host, port, factory, timeout=TCP_CONNECT_TIMEOUT) except JobTimeoutException: log.error("Failed to forward %s --> %s", hash_to_process[:8], host) return sys.exit(0) except Exception as err: log.exception("Job (pid %s) encountered unexpected error") return sys.exit(1)
def factory_setup_error(error): from twisted.internet import reactor log.error("Error when setting up factory:%s",error) reactor.fireSystemEvent("shutdown") return sys.exit(1)
def _addSystemEventTriggerTest(self, phase): eventType = 'test' events = [] def trigger(): events.append(None) self.addTrigger(phase, eventType, trigger) self.assertEqual(events, []) reactor.fireSystemEvent(eventType) self.assertEqual(events, [None])
def test_subsequentBeforeTriggerFiresPriorBeforeDeferred(self): """ If a trigger added to the C{'before'} phase of an event calls back a L{Deferred} returned by an earlier trigger in the C{'before'} phase of the same event, the remaining C{'before'} triggers for that event should be run and any further L{Deferred}s waited on before proceeding to the C{'during'} events. """ eventType = 'test' events = [] firstDeferred = Deferred() secondDeferred = Deferred() def firstBeforeTrigger(): return firstDeferred def secondBeforeTrigger(): firstDeferred.callback(None) def thirdBeforeTrigger(): events.append('before') return secondDeferred def duringTrigger(): events.append('during') self.addTrigger('before', eventType, firstBeforeTrigger) self.addTrigger('before', eventType, secondBeforeTrigger) self.addTrigger('before', eventType, thirdBeforeTrigger) self.addTrigger('during', eventType, duringTrigger) self.assertEqual(events, []) reactor.fireSystemEvent(eventType) self.assertEqual(events, ['before']) secondDeferred.callback(None) self.assertEqual(events, ['before', 'during'])
def _bail(self): from twisted.internet import reactor d = defer.Deferred() reactor.addSystemEventTrigger('after', 'shutdown', lambda: d.callback(None)) reactor.fireSystemEvent('shutdown') # radix's suggestion # As long as TestCase does crap stuff with the reactor we need to # manually shutdown the reactor here, and that requires util.wait # :( # so that the shutdown event completes unittest.TestCase('mktemp')._wait(d)
def test_interactionBetweenDifferentEvents(self): """ L{IReactorCore.fireSystemEvent} should behave the same way for a particular system event regardless of whether Deferreds are being waited on for a different system event. """ events = [] firstEvent = 'first-event' firstDeferred = Deferred() def beforeFirstEvent(): events.append(('before', 'first')) return firstDeferred def afterFirstEvent(): events.append(('after', 'first')) secondEvent = 'second-event' secondDeferred = Deferred() def beforeSecondEvent(): events.append(('before', 'second')) return secondDeferred def afterSecondEvent(): events.append(('after', 'second')) self.addTrigger('before', firstEvent, beforeFirstEvent) self.addTrigger('after', firstEvent, afterFirstEvent) self.addTrigger('before', secondEvent, beforeSecondEvent) self.addTrigger('after', secondEvent, afterSecondEvent) self.assertEqual(events, []) # After this, firstEvent should be stuck before 'during' waiting for # firstDeferred. reactor.fireSystemEvent(firstEvent) self.assertEqual(events, [('before', 'first')]) # After this, secondEvent should be stuck before 'during' waiting for # secondDeferred. reactor.fireSystemEvent(secondEvent) self.assertEqual(events, [('before', 'first'), ('before', 'second')]) # After this, firstEvent should have finished completely, but # secondEvent should be at the same place. firstDeferred.callback(None) self.assertEqual(events, [('before', 'first'), ('before', 'second'), ('after', 'first')]) # After this, secondEvent should have finished completely. secondDeferred.callback(None) self.assertEqual(events, [('before', 'first'), ('before', 'second'), ('after', 'first'), ('after', 'second')])