Python twisted.internet.reactor.callFromThread() Examples

The following are 30 code examples of twisted.internet.reactor.callFromThread(). 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.reactor , or try the search function .
Example #1
Source File: twistedtools.py    From locality-sensitive-hashing with MIT License 6 votes vote down vote up
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 
Example #2
Source File: kraken_wsclient_py.py    From kraken-wsclient-py with MIT License 6 votes vote down vote up
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) 
Example #3
Source File: test_threads.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
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) 
Example #4
Source File: test_threads.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
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 
Example #5
Source File: test_internet.py    From learn_python3_spider with MIT License 6 votes vote down vote up
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 
Example #6
Source File: test_threads.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
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) 
Example #7
Source File: twistedtools.py    From Computable with MIT License 6 votes vote down vote up
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 
Example #8
Source File: sqlwatch.py    From ccs-calendarserver with Apache License 2.0 6 votes vote down vote up
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 
Example #9
Source File: logging.py    From autopush with Mozilla Public License 2.0 6 votes vote down vote up
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 
Example #10
Source File: test_internet.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
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 
Example #11
Source File: twisted.py    From kotori with GNU Affero General Public License v3.0 6 votes vote down vote up
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)
        """ 
Example #12
Source File: start_server.py    From RAFCON with Eclipse Public License 1.0 6 votes vote down vote up
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) 
Example #13
Source File: start.py    From RAFCON with Eclipse Public License 1.0 6 votes vote down vote up
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) 
Example #14
Source File: test_threads.py    From learn_python3_spider with MIT License 6 votes vote down vote up
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) 
Example #15
Source File: test_threads.py    From learn_python3_spider with MIT License 6 votes vote down vote up
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 
Example #16
Source File: start.py    From RAFCON with Eclipse Public License 1.0 6 votes vote down vote up
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() 
Example #17
Source File: pamauth.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
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) 
Example #18
Source File: reactor.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def call_in_main(self, f, *args, **kwargs):
        """Cause a function to be executed by the reactor thread.

        @param f: The callable object to execute.
        @param args: The arguments to call it with.
        @param kwargs: The keyword arguments to call it with.

        @see: L{twisted.internet.interfaces.IReactorThreads.callFromThread}
        """
        self._reactor.callFromThread(f, *args, **kwargs) 
Example #19
Source File: watchdog.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, reactor=reactor, verbose=False, config=None,
                 broker=None, monitor=None, manager=None,
                 enabled_daemons=None):
        landscape_reactor = LandscapeReactor()
        if enabled_daemons is None:
            enabled_daemons = [Broker, Monitor, Manager]
        if broker is None and Broker in enabled_daemons:
            broker = Broker(
                RemoteBrokerConnector(landscape_reactor, config),
                verbose=verbose, config=config.config)
        if monitor is None and Monitor in enabled_daemons:
            monitor = Monitor(
                RemoteMonitorConnector(landscape_reactor, config),
                verbose=verbose, config=config.config)
        if manager is None and Manager in enabled_daemons:
            manager = Manager(
                RemoteManagerConnector(landscape_reactor, config),
                verbose=verbose, config=config.config)

        self.broker = broker
        self.monitor = monitor
        self.manager = manager
        self.daemons = [daemon
                        for daemon in [self.broker, self.monitor, self.manager]
                        if daemon]
        self.reactor = reactor
        self._checking = None
        self._stopping = False
        signal.signal(
            signal.SIGUSR1,
            lambda signal, frame: reactor.callFromThread(
                self._notify_rotate_logs))
        if config is not None and config.clones > 0:
            options = ["--clones", str(config.clones),
                       "--start-clones-over", str(config.start_clones_over)]
            for daemon in self.daemons:
                daemon.options = options

        self._ping_failures = {} 
Example #20
Source File: reactor.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
        from twisted.internet import reactor
        from twisted.internet.task import LoopingCall
        self._LoopingCall = LoopingCall
        self._reactor = reactor
        self._cleanup()
        self.callFromThread = reactor.callFromThread
        super(EventHandlingReactor, self).__init__() 
Example #21
Source File: service.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, config):
        self.config = config
        self.reactor = self.reactor_factory()
        if self.persist_filename:
            self.persist = get_versioned_persist(self)
        if not (self.config is not None and self.config.ignore_sigusr1):
            from twisted.internet import reactor
            signal.signal(
                signal.SIGUSR1,
                lambda signal, frame: reactor.callFromThread(rotate_logs)) 
Example #22
Source File: cl-zmq.py    From plugins with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def init(options, configuration, plugin, **kwargs):
    Setup.check_option_warnings(options, plugin)
    setup_dict = Setup.get_setup_dict(options)
    Setup.log_setup_dict(setup_dict, plugin)
    reactor.callFromThread(publisher.load_setup, setup_dict) 
Example #23
Source File: test_internet.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_lotsOfThreadsAreScheduledCorrectly(self):
        """
        L{IReactorThreads.callFromThread} can be used to schedule a large
        number of calls in the reactor thread.
        """
        def addAndMaybeFinish():
            self.counter += 1
            if self.counter == 100:
                self.deferred.callback(True)

        for i in range(100):
            self.schedule(addAndMaybeFinish)

        return self.deferred 
Example #24
Source File: test_internet.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def schedule(self, *args, **kwargs):
        """
        Override in subclasses.
        """
        reactor.callFromThread(*args, **kwargs) 
Example #25
Source File: test_internet.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def testCallFromThreadStops(self):
        """
        Ensure that callFromThread from inside a callFromThread
        callback doesn't sit in an infinite loop and lets other
        things happen too.
        """
        self.stopped = False
        d = defer.Deferred()
        reactor.callFromThread(self._callFromThreadCallback, d)
        return d 
Example #26
Source File: test_internet.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def testWakeUp(self):
        # Make sure other threads can wake up the reactor
        d = Deferred()
        def wake():
            time.sleep(0.1)
            # callFromThread will call wakeUp for us
            reactor.callFromThread(d.callback, None)
        reactor.callInThread(wake)
        return d 
Example #27
Source File: threads.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def blockingCallFromThread(reactor, f, *a, **kw):
    """
    Run a function in the reactor from a thread, and wait for the result
    synchronously.  If the function returns a L{Deferred}, wait for its
    result and return that.

    @param reactor: The L{IReactorThreads} provider which will be used to
        schedule the function call.
    @param f: the callable to run in the reactor thread
    @type f: any callable.
    @param a: the arguments to pass to C{f}.
    @param kw: the keyword arguments to pass to C{f}.

    @return: the result of the L{Deferred} returned by C{f}, or the result
        of C{f} if it returns anything other than a L{Deferred}.

    @raise: If C{f} raises a synchronous exception,
        C{blockingCallFromThread} will raise that exception.  If C{f}
        returns a L{Deferred} which fires with a L{Failure},
        C{blockingCallFromThread} will raise that failure's exception (see
        L{Failure.raiseException}).
    """
    queue = Queue.Queue()
    def _callFromThread():
        result = defer.maybeDeferred(f, *a, **kw)
        result.addBoth(queue.put)
    reactor.callFromThread(_callFromThread)
    result = queue.get()
    if isinstance(result, failure.Failure):
        result.raiseException()
    return result 
Example #28
Source File: threads.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def deferToThreadPool(reactor, threadpool, f, *args, **kwargs):
    """
    Call the function C{f} using a thread from the given threadpool and return
    the result as a Deferred.

    This function is only used by client code which is maintaining its own
    threadpool.  To run a function in the reactor's threadpool, use
    C{deferToThread}.

    @param reactor: The reactor in whose main thread the Deferred will be
        invoked.

    @param threadpool: An object which supports the C{callInThreadWithCallback}
        method of C{twisted.python.threadpool.ThreadPool}.

    @param f: The function to call.
    @param *args: positional arguments to pass to f.
    @param **kwargs: keyword arguments to pass to f.

    @return: A Deferred which fires a callback with the result of f, or an
        errback with a L{twisted.python.failure.Failure} if f throws an
        exception.
    """
    d = defer.Deferred()

    def onResult(success, result):
        if success:
            reactor.callFromThread(d.callback, result)
        else:
            reactor.callFromThread(d.errback, result)

    threadpool.callInThreadWithCallback(onResult, f, *args, **kwargs)

    return d 
Example #29
Source File: crawler.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _signal_kill(self, signum, _):
        install_shutdown_handlers(signal.SIG_IGN)
        signame = signal_names[signum]
        logger.info('Received %(signame)s twice, forcing unclean shutdown',
                    {'signame': signame})
        reactor.callFromThread(self._stop_reactor) 
Example #30
Source File: crawler.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _signal_shutdown(self, signum, _):
        install_shutdown_handlers(self._signal_kill)
        signame = signal_names[signum]
        logger.info("Received %(signame)s, shutting down gracefully. Send again to force ",
                    {'signame': signame})
        reactor.callFromThread(self._graceful_stop_reactor)