本文整理汇总了Python中twisted.internet.threads.blockingCallFromThread方法的典型用法代码示例。如果您正苦于以下问题:Python threads.blockingCallFromThread方法的具体用法?Python threads.blockingCallFromThread怎么用?Python threads.blockingCallFromThread使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.threads
的用法示例。
在下文中一共展示了threads.blockingCallFromThread方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def fetch(self, request_or_url, spider=None, redirect=True, **kwargs):
if isinstance(request_or_url, Request):
request = request_or_url
else:
url = any_to_uri(request_or_url)
request = Request(url, dont_filter=True, **kwargs)
if redirect:
request.meta['handle_httpstatus_list'] = SequenceExclude(range(300, 400))
else:
request.meta['handle_httpstatus_all'] = True
response = None
try:
response, spider = threads.blockingCallFromThread(
reactor, self._schedule, request, spider)
except IgnoreRequest:
pass
self.populate_vars(response, request, spider)
示例2: _input_code_with_completion
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def _input_code_with_completion(prompt, input_helper, reactor):
# reminder: this all occurs in a separate thread. All calls to input_helper
# must go through blockingCallFromThread()
c = CodeInputter(input_helper, reactor)
if readline is not None:
if readline.__doc__ and "libedit" in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
readline.set_completer(c.completer)
readline.set_completer_delims("")
debug("==== readline-based completion is prepared")
else:
debug("==== unable to import readline, disabling completion")
code = input(prompt)
# Code is str(bytes) on py2, and str(unicode) on py3. We want unicode.
if isinstance(code, bytes):
code = code.decode("utf-8")
c.finish(code)
return c.used_completion
示例3: _eval
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def _eval(deferred, reactor):
"""Evaluate a deferred on a given reactor and return the result
This function is safe to call with a deferred that has already been evaluated.
"""
@defer.inlineCallbacks
def closure():
if deferred.called:
result = deferred.result
else:
result = yield deferred
defer.returnValue(result)
if threading.currentThread().getName() == reactor.thread_name:
return closure()
else:
return threads.blockingCallFromThread(reactor, closure)
#
# Exceptions
#
示例4: connect
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def connect(self):
"""Connect to wpa_supplicant over D-Bus
:returns: Remote D-Bus proxy object of the root wpa_supplicant interface
:rtype: :class:`~WpaSupplicant`
"""
if not self._reactor.running:
raise ReactorNotRunning('Twisted Reactor must be started (call .run())')
@defer.inlineCallbacks
def get_conn():
self._reactor.thread_name = threading.currentThread().getName()
conn = yield client.connect(self._reactor, busAddress='system')
defer.returnValue(conn)
conn = threads.blockingCallFromThread(self._reactor, get_conn)
return WpaSupplicant('/fi/w1/wpa_supplicant1', conn, self._reactor, )
示例5: blocking_call_from_thread
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def blocking_call_from_thread(self, callback, timeout):
"""Call the given function from a thread, and wait for the result synchronously
for as long as the timeout will allow.
Args:
callback: Callable function to be invoked from the thread.
timeout (:obj: int): Number of seconds to wait for the response before
raising an exception.
Returns:
The results from the callback, or a timeout exception.
"""
result_placeholder = defer.Deferred()
if timeout:
result_placeholder.addTimeout(timeout, reactor, onTimeoutCancel=self.raise_timeout_exception)
return threads.blockingCallFromThread(reactor, callback, result_placeholder)
示例6: _clean
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def _clean(self):
# Spin a bit to flush out imminent delayed calls. It's not clear why
# we do this: left-over delayed calls are detritus like any other.
# However, this is done by both testtools and Twisted's trial, and we
# do it for consistency with them.
if not self._tickReactor(0.5):
raise MAASCrochetReactorStalled(
"Reactor did not respond after 500ms."
)
# Ensure that all threadpools are quiet. Do this first because we must
# crash the whole run if they don't go quiet before the next test.
dirtyPools, threadStacks = blockingCallFromThread(
reactor, self._cleanThreads
)
if len(dirtyPools) != 0:
raise MAASCrochetDirtyThreadsError(dirtyPools, threadStacks)
# Find leftover delayed calls and selectables in use.
dirtyCalls, dirtySelectables = blockingCallFromThread(
reactor, self._cleanReactor
)
if len(dirtyCalls) != 0 or len(dirtySelectables) != 0:
raise MAASCrochetDirtyReactorError(dirtyCalls, dirtySelectables)
示例7: _execute
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def _execute(self):
"""
Callback fired when the associated event is set. Run the C{action}
callback on the wrapped descriptor in the main reactor thread and raise
or return whatever it raises or returns to cause this event handler to
be removed from C{self._reactor} if appropriate.
"""
return blockingCallFromThread(
self._reactor, lambda: getattr(self._fd, self._action)())
示例8: _testBlockingCallFromThread
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def _testBlockingCallFromThread(self, reactorFunc):
"""
Utility method to test L{threads.blockingCallFromThread}.
"""
waiter = threading.Event()
results = []
errors = []
def cb1(ign):
def threadedFunc():
try:
r = threads.blockingCallFromThread(reactor, reactorFunc)
except Exception as e:
errors.append(e)
else:
results.append(r)
waiter.set()
reactor.callInThread(threadedFunc)
return threads.deferToThread(waiter.wait, self.getTimeout())
def cb2(ign):
if not waiter.isSet():
self.fail("Timed out waiting for event")
return results, errors
return self._waitForThread().addCallback(cb1).addBoth(cb2)
示例9: test_blockingCallFromThread
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def test_blockingCallFromThread(self):
"""
Test blockingCallFromThread facility: create a thread, call a function
in the reactor using L{threads.blockingCallFromThread}, and verify the
result returned.
"""
def reactorFunc():
return defer.succeed("foo")
def cb(res):
self.assertEqual(res[0][0], "foo")
return self._testBlockingCallFromThread(reactorFunc).addCallback(cb)
示例10: test_asyncBlockingCallFromThread
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def test_asyncBlockingCallFromThread(self):
"""
Test blockingCallFromThread as above, but be sure the resulting
Deferred is not already fired.
"""
def reactorFunc():
d = defer.Deferred()
reactor.callLater(0.1, d.callback, "egg")
return d
def cb(res):
self.assertEqual(res[0][0], "egg")
return self._testBlockingCallFromThread(reactorFunc).addCallback(cb)
示例11: test_asyncErrorBlockingCallFromThread
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def test_asyncErrorBlockingCallFromThread(self):
"""
Test error report for blockingCallFromThread as above, but be sure the
resulting Deferred is not already fired.
"""
def reactorFunc():
d = defer.Deferred()
reactor.callLater(0.1, d.errback, RuntimeError("spam"))
return d
def cb(res):
self.assertIsInstance(res[1][0], RuntimeError)
self.assertEqual(res[1][0].args[0], "spam")
return self._testBlockingCallFromThread(reactorFunc).addCallback(cb)
示例12: test_errorBlockingCallFromThread
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def test_errorBlockingCallFromThread(self):
"""
Test error report for blockingCallFromThread.
"""
def reactorFunc():
return defer.fail(RuntimeError("bar"))
def cb(res):
self.assertIsInstance(res[1][0], RuntimeError)
self.assertEqual(res[1][0].args[0], "bar")
return self._testBlockingCallFromThread(reactorFunc).addCallback(cb)
示例13: bcft
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def bcft(self, f, *a, **kw):
return blockingCallFromThread(self._reactor, f, *a, **kw)
示例14: _testBlockingCallFromThread
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def _testBlockingCallFromThread(self, reactorFunc):
"""
Utility method to test L{threads.blockingCallFromThread}.
"""
waiter = threading.Event()
results = []
errors = []
def cb1(ign):
def threadedFunc():
try:
r = threads.blockingCallFromThread(reactor, reactorFunc)
except Exception, e:
errors.append(e)
else:
示例15: test_blockingCallFromThread
# 需要导入模块: from twisted.internet import threads [as 别名]
# 或者: from twisted.internet.threads import blockingCallFromThread [as 别名]
def test_blockingCallFromThread(self):
"""
Test blockingCallFromThread facility: create a thread, call a function
in the reactor using L{threads.blockingCallFromThread}, and verify the
result returned.
"""
def reactorFunc():
return defer.succeed("foo")
def cb(res):
self.assertEquals(res[0][0], "foo")
return self._testBlockingCallFromThread(reactorFunc).addCallback(cb)