Python twisted.internet.threads.blockingCallFromThread() Examples

The following are 30 code examples of twisted.internet.threads.blockingCallFromThread(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.internet.threads , or try the search function .
Example #1
Source File: _rlcompleter.py    From magic-wormhole with MIT License 6 votes vote down vote up
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 
Example #2
Source File: core.py    From python-wpa-supplicant with Mozilla Public License 2.0 6 votes vote down vote up
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
# 
Example #3
Source File: shell.py    From learn_python3_spider with MIT License 6 votes vote down vote up
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) 
Example #4
Source File: core.py    From python-wpa-supplicant with Mozilla Public License 2.0 6 votes vote down vote up
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, ) 
Example #5
Source File: shell.py    From learn_python3_spider with MIT License 6 votes vote down vote up
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) 
Example #6
Source File: runtest.py    From maas with GNU Affero General Public License v3.0 6 votes vote down vote up
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) 
Example #7
Source File: comm_autobahn.py    From roslibpy with MIT License 6 votes vote down vote up
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) 
Example #8
Source File: dht_server.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def main(iface):
    ret = 0
    try:
        a = AutoDHTServer()
        a.start(iface)

        b = AutoDHTServer()
        b.start(iface)

        time.sleep(4)

        print a.set(key="APA", value="banan")

        print a.get(key="APA")
        print b.get(key="APA")

        a.stop()
        b.stop()

    except:
        traceback.print_exc()
        ret = 1

    finally:
        if reactor.running:
            threads.blockingCallFromThread(reactor, reactor.stop)

    return ret 
Example #9
Source File: test_threads.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_errorBlockingCallFromThread(self):
        """
        Test error report for blockingCallFromThread.
        """
        def reactorFunc():
            return defer.fail(RuntimeError("bar"))
        def cb(res):
            self.assert_(isinstance(res[1][0], RuntimeError))
            self.assertEquals(res[1][0].args[0], "bar")

        return self._testBlockingCallFromThread(reactorFunc).addCallback(cb) 
Example #10
Source File: test_threads.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
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.assert_(isinstance(res[1][0], RuntimeError))
            self.assertEquals(res[1][0].args[0], "spam")

        return self._testBlockingCallFromThread(reactorFunc).addCallback(cb) 
Example #11
Source File: dht_server.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def __init__(self, obj, *args, **kwargs):
        self._obj = threads.blockingCallFromThread(reactor, obj, *args, **kwargs) 
Example #12
Source File: dht_server.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def _call(self, func, *args, **kwargs):
        return threads.blockingCallFromThread(reactor, func, *args, **kwargs) 
Example #13
Source File: test_threads.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
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: 
Example #14
Source File: test_append.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def start(self, port=0, boot_strap=[]):
        self.kserver = self.server_type()
        self.kserver.bootstrap(boot_strap)

        self.port = threads.blockingCallFromThread(reactor, reactor.listenUDP, port, self.kserver.protocol)
        print "Starting server:", self.port

        time.sleep(.2)

        return self.port.getHost().port, self.kserver 
Example #15
Source File: test_append.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def stop(self):
        result = threads.blockingCallFromThread(reactor, self.port.stopListening) 
Example #16
Source File: dht_server.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def _call(self, func, *args, **kwargs):
        return threads.blockingCallFromThread(reactor, func, *args, **kwargs) 
Example #17
Source File: dht_server.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def main(iface):
    ret = 0
    try:
        a = AutoDHTServer()
        a.start(iface)

        b = AutoDHTServer()
        b.start(iface)

        time.sleep(4)

        print a.set(key="APA", value="banan")

        print a.get(key="APA")
        print b.get(key="APA")

        a.stop()
        b.stop()

    except:
        traceback.print_exc()
        ret = 1

    finally:
        if reactor.running:
            threads.blockingCallFromThread(reactor, reactor.stop)

    return ret 
Example #18
Source File: test_secappend.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def start(self, port=0, boot_strap=[]):
        self.kserver = self.server_type()
        self.kserver.bootstrap(boot_strap)

        self.port = threads.blockingCallFromThread(reactor, reactor.listenUDP, port, self.kserver.protocol)
        print "Starting server:", self.port

        time.sleep(.2)

        return self.port.getHost().port, self.kserver 
Example #19
Source File: test_secappend.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def stop(self):
        result = threads.blockingCallFromThread(reactor, self.port.stopListening) 
Example #20
Source File: test_threads.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
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.assertEquals(res[0][0], "egg")

        return self._testBlockingCallFromThread(reactorFunc).addCallback(cb) 
Example #21
Source File: test_threads.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
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) 
Example #22
Source File: _rlcompleter.py    From magic-wormhole with MIT License 5 votes vote down vote up
def bcft(self, f, *a, **kw):
        return blockingCallFromThread(self._reactor, f, *a, **kw) 
Example #23
Source File: test_threads.py    From learn_python3_spider with MIT License 5 votes vote down vote up
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) 
Example #24
Source File: test_threads.py    From learn_python3_spider with MIT License 5 votes vote down vote up
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) 
Example #25
Source File: test_threads.py    From learn_python3_spider with MIT License 5 votes vote down vote up
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) 
Example #26
Source File: test_threads.py    From learn_python3_spider with MIT License 5 votes vote down vote up
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) 
Example #27
Source File: win32eventreactor.py    From learn_python3_spider with MIT License 5 votes vote down vote up
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)()) 
Example #28
Source File: test_threads.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
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) 
Example #29
Source File: test_threads.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
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) 
Example #30
Source File: test_threads.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
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)