本文整理汇总了Python中twisted.internet.reactor.addSystemEventTrigger方法的典型用法代码示例。如果您正苦于以下问题:Python reactor.addSystemEventTrigger方法的具体用法?Python reactor.addSystemEventTrigger怎么用?Python reactor.addSystemEventTrigger使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.reactor
的用法示例。
在下文中一共展示了reactor.addSystemEventTrigger方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
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)
示例2: run
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def run():
config.read()
logs.api()
top_service = service.MultiService()
db = Db()
datalib.db = db
db.setServiceParent(top_service)
http_service = internet.TCPServer(config.HTTP_PORT, Site(db), interface=config.HTTP_ADDR)
http_service.setServiceParent(top_service)
top_service.startService()
reactor.addSystemEventTrigger('before', 'shutdown', top_service.stopService)
reactor.run()
示例3: main
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def main(number):
def get_metrics():
return [
("checker.time.%s.%s" %
(config.HOSTNAME,
number),
spy.TRIGGER_CHECK.get_metrics()["sum"]),
("checker.triggers.%s.%s" %
(config.HOSTNAME,
number),
spy.TRIGGER_CHECK.get_metrics()["count"]),
("checker.errors.%s.%s" %
(config.HOSTNAME,
number),
spy.TRIGGER_CHECK_ERRORS.get_metrics()["count"])]
graphite.sending(get_metrics)
def start(db):
checker = TriggersCheck(db)
checker.start()
reactor.addSystemEventTrigger('before', 'shutdown', checker.stop)
run(start)
示例4: run
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def run():
config.read()
logs.checker_master()
if config.ARGS.t:
check(config.ARGS.t)
return
top_service = TopService()
db = Db()
datalib.db = db
db.setServiceParent(top_service)
sub_service = MasterService(db)
sub_service.setServiceParent(top_service)
top_service.startService()
reactor.addSystemEventTrigger('before', 'shutdown', top_service.stopService)
reactor.run()
示例5: connect_with_retry
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def connect_with_retry(self):
try:
self.client.connect(self.broker_host, port=self.broker_port, keepalive=60)
self.connect_loop.stop()
except:
log.failure(u'Error connecting to MQTT broker but retrying each {retry_interval} seconds',
retry_interval=self.retry_interval)
return
"""
This is part of the threaded client interface. Call this once to
start a new thread to process network traffic. This provides an
alternative to repeatedly calling loop() yourself.
"""
self.client.loop_start()
reactor.addSystemEventTrigger('before', 'shutdown', self.client.loop_stop, True)
# The callback for when the client receives a CONNACK response from the server.
示例6: test_singleStopErrback
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def test_singleStopErrback(self):
"""
L{task.react} doesn't try to stop the reactor if the L{defer.Deferred}
the function it is passed is errbacked after the reactor has already
been stopped.
"""
class ExpectedException(Exception):
pass
def main(reactor):
reactor.callLater(1, reactor.stop)
finished = defer.Deferred()
reactor.addSystemEventTrigger(
'during', 'shutdown', finished.errback, ExpectedException())
return finished
r = _FakeReactor()
exitError = self.assertRaises(
SystemExit, task.react, main, _reactor=r)
self.assertEqual(1, exitError.code)
self.assertEqual(r.seconds(), 1)
errors = self.flushLoggedErrors(ExpectedException)
self.assertEqual(len(errors), 1)
示例7: test_beforePreceedsDuring
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
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'])
示例8: test_duringPreceedsAfter
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
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'])
示例9: test_removeNonExistentSystemEventTrigger
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def test_removeNonExistentSystemEventTrigger(self):
"""
Passing an object to L{IReactorCore.removeSystemEventTrigger} which was
not returned by a previous call to
L{IReactorCore.addSystemEventTrigger} or which has already been passed
to C{removeSystemEventTrigger} should result in L{TypeError},
L{KeyError}, or L{ValueError} being raised.
"""
b = self.addTrigger('during', 'test', lambda: None)
self.removeTrigger(b)
self.assertRaises(
TypeError, reactor.removeSystemEventTrigger, None)
self.assertRaises(
ValueError, reactor.removeSystemEventTrigger, b)
self.assertRaises(
KeyError,
reactor.removeSystemEventTrigger,
(b[0], ('xxx',) + b[1][1:]))
示例10: configure
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def configure(protocol, port, pipes, interface):
remove_all()
reactor.addSystemEventTrigger('after', 'shutdown', remove_all)
# gets default (outward-facing) network interface (e.g. deciding which of
# eth0, eth1, wlan0 is being used by the system to connect to the internet)
if interface == "auto":
interface = netifaces.gateways()['default'][netifaces.AF_INET][1]
else:
if interface not in netifaces.interfaces():
raise ValueError("Given interface does not exist.", interface)
add(protocol, port, interface)
manager = libnetfilter_queue.Manager()
manager.bind(UP_QUEUE, packet_handler(manager, pipes.up))
manager.bind(DOWN_QUEUE, packet_handler(manager, pipes.down))
reader = abstract.FileDescriptor()
reader.doRead = manager.process
reader.fileno = lambda: manager.fileno
reactor.addReader(reader)
示例11: start
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def start(self, stop_after_crawl=True):
"""
This method starts a Twisted `reactor`_, adjusts its pool size to
:setting:`REACTOR_THREADPOOL_MAXSIZE`, and installs a DNS cache based
on :setting:`DNSCACHE_ENABLED` and :setting:`DNSCACHE_SIZE`.
If ``stop_after_crawl`` is True, the reactor will be stopped after all
crawlers have finished, using :meth:`join`.
:param boolean stop_after_crawl: stop or not the reactor when all
crawlers have finished
"""
if stop_after_crawl:
d = self.join()
# Don't start the reactor if the deferreds are already fired
if d.called:
return
d.addBoth(self._stop_reactor)
reactor.installResolver(self._get_dns_resolver())
tp = reactor.getThreadPool()
tp.adjustPoolsize(maxthreads=self.settings.getint('REACTOR_THREADPOOL_MAXSIZE'))
reactor.addSystemEventTrigger('before', 'shutdown', self.stop)
reactor.run(installSignalHandlers=False) # blocking call
示例12: test_singleStopCallback
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def test_singleStopCallback(self):
"""
L{task.react} doesn't try to stop the reactor if the L{defer.Deferred}
the function it is passed is called back after the reactor has already
been stopped.
"""
def main(reactor):
reactor.callLater(1, reactor.stop)
finished = defer.Deferred()
reactor.addSystemEventTrigger(
'during', 'shutdown', finished.callback, None)
return finished
r = _FakeReactor()
exitError = self.assertRaises(
SystemExit, task.react, main, _reactor=r)
self.assertEqual(r.seconds(), 1)
self.assertEqual(0, exitError.code)
示例13: __new__
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def __new__(cls, *args, **kw):
if not hasattr(cls, '_instance'):
instance = super(Controller, cls).__new__(cls)
instance._allocated_pins = {}
instance._poll_queue = select.epoll()
instance._available_pins = []
instance._running = True
# Cleanup before stopping reactor
reactor.addSystemEventTrigger('before', 'shutdown', instance.stop)
# Run the EPoll in a Thread, as it blocks.
reactor.callInThread(instance._poll_queue_loop)
cls._instance = instance
return cls._instance
示例14: run
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def run(self, save=1, installSignalHandlers=1):
"""run(save=1, installSignalHandlers=1)
Run this application, running the main loop if necessary.
If 'save' is true, then when this Application is shut down, it
will be persisted to a pickle.
'installSignalHandlers' is passed through to reactor.run(), the
function that starts the mainloop.
"""
from twisted.internet import reactor
if not self._boundPorts:
self.bindPorts()
self._save = save
reactor.addSystemEventTrigger('before', 'shutdown', self._beforeShutDown)
reactor.addSystemEventTrigger('after', 'shutdown', self._afterShutDown)
global theApplication
theApplication = self
log.callWithLogger(self, reactor.run, installSignalHandlers=installSignalHandlers)
#
# These are dummy classes for backwards-compatibility!
#
示例15: listen
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import addSystemEventTrigger [as 别名]
def listen(configManager):
# Things get messy if pdconfd is restarted with running chutes. Then it
# will try to reconfigure the system. One easy solution is to unload the
# configuration before exiting.
reactor.addSystemEventTrigger('before', 'shutdown',
configManager.unload)
# Now load all of the configuration for the first time.
configManager.loadConfig()