本文整理汇总了Python中twisted.internet.reactor.callFromThread方法的典型用法代码示例。如果您正苦于以下问题:Python reactor.callFromThread方法的具体用法?Python reactor.callFromThread怎么用?Python reactor.callFromThread使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.reactor
的用法示例。
在下文中一共展示了reactor.callFromThread方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: stop_reactor
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def stop_reactor():
"""Stop the reactor and join the reactor thread until it stops.
Call this function in teardown at the module or package level to
reset the twisted system after your tests. You *must* do this if
you mix tests using these tools and tests using twisted.trial.
"""
global _twisted_thread
def stop_reactor():
'''Helper for calling stop from withing the thread.'''
reactor.stop()
reactor.callFromThread(stop_reactor)
reactor_thread.join()
for p in reactor.getDelayedCalls():
if p.active():
p.cancel()
_twisted_thread = None
示例2: subscribe
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def subscribe(self, *args):
#d = self.protocol.subscribe("foo/bar/baz", 0)
log.info(u"Subscribing to topics {subscriptions}. protocol={protocol}", subscriptions=self.subscriptions, protocol=self.protocol)
for topic in self.subscriptions:
log.info(u"Subscribing to topic '{topic}'", topic=topic)
# Topic name **must not** be unicode, so casting to string
e = self.protocol.subscribe(str(topic), 0)
log.info(u"Setting callback handler: {callback}", callback=self.callback)
self.protocol.setPublishHandler(self.on_message_twisted)
"""
def cb(*args, **kwargs):
log.info('publishHandler got called: name={name}, args={args}, kwargs={kwargs}', name=self.name, args=args, kwargs=kwargs)
return reactor.callFromThread(self.callback, *args, **kwargs)
self.protocol.setPublishHandler(cb)
"""
示例3: _start_socket
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def _start_socket(self, id_, payload, callback, private=False):
if id_ in self._conns:
return False
if private:
factory_url = self.PRIVATE_STREAM_URL
else:
factory_url = self.STREAM_URL
factory = KrakenClientFactory(factory_url, payload=payload)
factory.base_client = self
factory.protocol = KrakenClientProtocol
factory.callback = callback
factory.reconnect = True
self.factories[id_] = factory
reactor.callFromThread(self.add_connection, id_, factory_url)
示例4: test_threadsAreRunInScheduledOrder
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def test_threadsAreRunInScheduledOrder(self):
"""
Callbacks should be invoked in the order they were scheduled.
"""
order = []
def check(_):
self.assertEqual(order, [1, 2, 3])
self.deferred.addCallback(check)
self.schedule(order.append, 1)
self.schedule(order.append, 2)
self.schedule(order.append, 3)
self.schedule(reactor.callFromThread, self.deferred.callback, None)
return self.deferred
示例5: test_callFromThread
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def test_callFromThread(self):
"""
Test callFromThread functionality: from the main thread, and from
another thread.
"""
def cb(ign):
firedByReactorThread = defer.Deferred()
firedByOtherThread = defer.Deferred()
def threadedFunc():
reactor.callFromThread(firedByOtherThread.callback, None)
reactor.callInThread(threadedFunc)
reactor.callFromThread(firedByReactorThread.callback, None)
return defer.DeferredList(
[firedByReactorThread, firedByOtherThread],
fireOnOneErrback=True)
return self._waitForThread().addCallback(cb)
示例6: test_wakerOverflow
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def test_wakerOverflow(self):
"""
Try to make an overflow on the reactor waker using callFromThread.
"""
def cb(ign):
self.failure = None
waiter = threading.Event()
def threadedFunction():
# Hopefully a hundred thousand queued calls is enough to
# trigger the error condition
for i in xrange(100000):
try:
reactor.callFromThread(lambda: None)
except:
self.failure = failure.Failure()
break
waiter.set()
reactor.callInThread(threadedFunction)
waiter.wait(120)
if not waiter.isSet():
self.fail("Timed out waiting for event")
if self.failure is not None:
return defer.fail(self.failure)
return self._waitForThread().addCallback(cb)
示例7: test_callMultiple
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def test_callMultiple(self):
"""
L{threads.callMultipleInThread} calls multiple functions in a thread.
"""
L = []
N = 10
d = defer.Deferred()
def finished():
self.assertEqual(L, list(range(N)))
d.callback(None)
threads.callMultipleInThread([
(L.append, (i,), {}) for i in xrange(N)
] + [(reactor.callFromThread, (finished,), {})])
return d
示例8: raven_log
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def raven_log(self, event):
f = event["log_failure"]
stack = None
extra = dict()
tb = f.getTracebackObject()
if not tb:
# include the current stack for at least some
# context. sentry's expecting that "Frames should be
# sorted from oldest to newest."
stack = list(iter_stack_frames())[:-5] # approx.
extra = dict(no_failure_tb=True)
extra.update(
log_format=event.get('log_format'),
log_namespace=event.get('log_namespace'),
client_info=event.get('client_info'),
)
reactor.callFromThread(
self.raven_client.captureException,
exc_info=(f.type, f.value, tb),
stack=stack,
extra=extra,
)
# just in case
del tb
示例9: check_for_sm_finished
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def check_for_sm_finished(sm, monitoring_manager=None):
from rafcon.core.states.state import StateExecutionStatus
while sm.root_state.state_execution_status is not StateExecutionStatus.INACTIVE:
try:
sm.root_state.concurrency_queue.get(timeout=10.0)
except Empty as e:
pass
# no logger output here to make it easier for the parser
print("RAFCON live signal")
sm.root_state.join()
# stop the network if the monitoring plugin is enabled
if monitoring_manager:
from twisted.internet import reactor
reactor.callFromThread(reactor.stop)
示例10: start_stop_state_machine
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def start_stop_state_machine(state_machine, start_state_path, quit_flag):
from rafcon.utils.gui_functions import call_gui_callback
state_machine_execution_engine = core_singletons.state_machine_execution_engine
call_gui_callback(
state_machine_execution_engine.execute_state_machine_from_path,
state_machine=state_machine,
start_state_path=start_state_path,
wait_for_execution_finished=True
)
if reactor_required():
from twisted.internet import reactor
reactor.callFromThread(reactor.stop)
if quit_flag:
gui_singletons.main_window_controller.get_controller('menu_bar_controller').on_quit_activate(None, None)
示例11: signal_handler
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def signal_handler(signal, frame):
global _user_abort
state_machine_execution_engine = core_singletons.state_machine_execution_engine
core_singletons.shut_down_signal = signal
logger.info("Shutting down ...")
try:
if not state_machine_execution_engine.finished_or_stopped():
state_machine_execution_engine.stop()
state_machine_execution_engine.join(3) # Wait max 3 sec for the execution to stop
except Exception:
logger.exception("Could not stop state machine")
_user_abort = True
# shutdown twisted correctly
if reactor_required():
from twisted.internet import reactor
if reactor.running:
plugins.run_hook("pre_destruction")
reactor.callFromThread(reactor.stop)
logging.shutdown()
示例12: test_callMultiple
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def test_callMultiple(self):
"""
L{threads.callMultipleInThread} calls multiple functions in a thread.
"""
L = []
N = 10
d = defer.Deferred()
def finished():
self.assertEqual(L, list(range(N)))
d.callback(None)
threads.callMultipleInThread([
(L.append, (i,), {}) for i in range(N)
] + [(reactor.callFromThread, (finished,), {})])
return d
示例13: waitForInterrupt
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def waitForInterrupt():
if signal.getsignal(signal.SIGINT) != signal.default_int_handler:
raise RuntimeError("Already waiting")
d = Deferred()
def fire(*ignored):
global interrupted
signal.signal(signal.SIGINT, signal.default_int_handler)
now = time.time()
if now - interrupted < 4:
reactor.callFromThread(lambda: d.errback(Failure(Stop())))
else:
interrupted = now
reactor.callFromThread(d.callback, None)
signal.signal(signal.SIGINT, fire)
return d
示例14: pamAuthenticateThread
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def pamAuthenticateThread(service, user, conv):
def _conv(items):
from twisted.internet import reactor
try:
d = conv(items)
except:
import traceback
traceback.print_exc()
return
ev = threading.Event()
def cb(r):
ev.r = (1, r)
ev.set()
def eb(e):
ev.r = (0, e)
ev.set()
reactor.callFromThread(d.addCallbacks, cb, eb)
ev.wait()
done = ev.r
if done[0]:
return done[1]
else:
raise done[1].type, done[1].value
return callIntoPAM(service, user, _conv)
示例15: run_thread
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import callFromThread [as 别名]
def run_thread(execute=True):
"""
Start pdconfd service as a thread.
This function schedules pdconfd to run as a thread and returns immediately.
"""
global configManager
configManager = ConfigManager(settings.PDCONFD_WRITE_DIR, execute)
reactor.callFromThread(listen, configManager)