當前位置: 首頁>>代碼示例>>Python>>正文


Python threads.blockingCallFromThread方法代碼示例

本文整理匯總了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) 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:19,代碼來源:shell.py

示例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 
開發者ID:warner,項目名稱:magic-wormhole,代碼行數:22,代碼來源:_rlcompleter.py

示例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
# 
開發者ID:digidotcom,項目名稱:python-wpa-supplicant,代碼行數:26,代碼來源:core.py

示例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, ) 
開發者ID:digidotcom,項目名稱:python-wpa-supplicant,代碼行數:20,代碼來源:core.py

示例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) 
開發者ID:gramaziokohler,項目名稱:roslibpy,代碼行數:18,代碼來源:comm_autobahn.py

示例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) 
開發者ID:maas,項目名稱:maas,代碼行數:24,代碼來源:runtest.py

示例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)()) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:11,代碼來源:win32eventreactor.py

示例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) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:28,代碼來源:test_threads.py

示例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) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:14,代碼來源:test_threads.py

示例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) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:15,代碼來源:test_threads.py

示例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) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:16,代碼來源:test_threads.py

示例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) 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:13,代碼來源:test_threads.py

示例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) 
開發者ID:warner,項目名稱:magic-wormhole,代碼行數:4,代碼來源:_rlcompleter.py

示例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: 
開發者ID:kuri65536,項目名稱:python-for-android,代碼行數:16,代碼來源:test_threads.py

示例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) 
開發者ID:kuri65536,項目名稱:python-for-android,代碼行數:14,代碼來源:test_threads.py


注:本文中的twisted.internet.threads.blockingCallFromThread方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。