Python twisted.internet.reactor.callInThread() Examples

The following are 30 code examples of twisted.internet.reactor.callInThread(). 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: connection_mgr.py    From voltha with Apache License 2.0 6 votes vote down vote up
def start(self):

        if self.running:
            return

        self.log.debug('starting')

        self.running = True

        # Start monitoring the vcore grpc channel
        reactor.callInThread(self.monitor_vcore_grpc_channel)

        # Start monitoring logical devices and manage agents accordingly
        reactor.callLater(0, self.monitor_logical_devices)

        self.log.info('started')

        return self 
Example #2
Source File: test_spinner.py    From pth-toolkit with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_clean_running_threads(self):
        import threading
        import time
        current_threads = list(threading.enumerate())
        reactor = self.make_reactor()
        timeout = self.make_timeout()
        spinner = self.make_spinner(reactor)
        spinner.run(timeout, reactor.callInThread, time.sleep, timeout / 2.0)
        # Python before 2.5 has a race condition with thread handling where
        # join() does not remove threads from enumerate before returning - the
        # thread being joined does the removal. This was fixed in Python 2.5
        # but we still support 2.4, so we have to workaround the issue.
        # http://bugs.python.org/issue1703448.
        self.assertThat(
            [thread for thread in threading.enumerate() if thread.isAlive()],
            Equals(current_threads)) 
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_callInThread(self):
        """
        Test callInThread functionality: set a C{threading.Event}, and check
        that it's not in the main thread.
        """
        def cb(ign):
            waiter = threading.Event()
            result = []
            def threadedFunc():
                result.append(threadable.isInIOThread())
                waiter.set()

            reactor.callInThread(threadedFunc)
            waiter.wait(120)
            if not waiter.isSet():
                self.fail("Timed out waiting for event.")
            else:
                self.assertEqual(result, [False])
        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_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 #5
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 #6
Source File: test_threads.py    From python-for-android with Apache License 2.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 #7
Source File: test_threads.py    From python-for-android with Apache License 2.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 #8
Source File: test_threads.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def test_callInThread(self):
        """
        Test callInThread functionality: set a C{threading.Event}, and check
        that it's not in the main thread.
        """
        def cb(ign):
            waiter = threading.Event()
            result = []
            def threadedFunc():
                result.append(threadable.isInIOThread())
                waiter.set()

            reactor.callInThread(threadedFunc)
            waiter.wait(120)
            if not waiter.isSet():
                self.fail("Timed out waiting for event.")
            else:
                self.assertEquals(result, [False])
        return self._waitForThread().addCallback(cb) 
Example #9
Source File: gpio.py    From python-sysfs-gpio with MIT License 6 votes vote down vote up
def __new__(cls, *args, **kw):
        if not hasattr(cls, '_instance'):
            instance = super(Controller, cls).__new__(cls)
            instance._allocated_pins = {}
            instance._poll_queue = select.epoll()

            instance._available_pins = []
            instance._running = True

            # Cleanup before stopping reactor
            reactor.addSystemEventTrigger('before', 'shutdown', instance.stop)

            # Run the EPoll in a Thread, as it blocks.
            reactor.callInThread(instance._poll_queue_loop)

            cls._instance = instance
        return cls._instance 
Example #10
Source File: test_threads.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def testWakerOverflow(self):
        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) 
Example #11
Source File: test_threads.py    From learn_python3_spider with MIT License 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 range(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 #12
Source File: test_threads.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_callInThread(self):
        """
        Test callInThread functionality: set a C{threading.Event}, and check
        that it's not in the main thread.
        """
        def cb(ign):
            waiter = threading.Event()
            result = []
            def threadedFunc():
                result.append(threadable.isInIOThread())
                waiter.set()

            reactor.callInThread(threadedFunc)
            waiter.wait(120)
            if not waiter.isSet():
                self.fail("Timed out waiting for event.")
            else:
                self.assertEqual(result, [False])
        return self._waitForThread().addCallback(cb) 
Example #13
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 #14
Source File: plugin.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def makeService(self, options, clock=reactor):
        """Construct the MAAS Cluster service."""
        register_sigusr1_toggle_cprofile("rackd")
        register_sigusr2_thread_dump_handler()
        clean_prometheus_dir()
        add_patches_to_txtftp()
        add_patches_to_twisted()

        self._loadSettings()
        self._configureCrochet()
        if settings.DEBUG:
            # Always log at debug level in debug mode.
            self._configureLogging(3)
        else:
            self._configureLogging(options["verbosity"])

        with ClusterConfiguration.open() as config:
            tftp_root = config.tftp_root
            tftp_port = config.tftp_port

        from provisioningserver import services

        for service in self._makeServices(tftp_root, tftp_port, clock=clock):
            service.setServiceParent(services)

        reactor.callInThread(generate_certificate_if_needed)
        return services 
Example #15
Source File: threading_latency.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def printResult(self):
        print
        print
        print "callFromThread latency:"
        sum = 0
        for t in self.from_times: sum += t
        print "%f millisecond" % ((sum / self.numRounds) * 1000)

        print "callInThread latency:"
        sum = 0
        for t in self.in_times: sum += t
        print "%f millisecond" % ((sum / self.numRounds) * 1000)
        print
        print 
Example #16
Source File: test_calvin_transport.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def _base_run(self):
        # make it work with twisted py.test plugin also
        reactor._started = False
        print "timeout %s", self._timeout
        reactor.callLater(self._timeout, self._stop_reactor, timeout=True)
        reactor.callInThread(self._read_thread)
        reactor.run() 
Example #17
Source File: comm_autobahn.py    From roslibpy with MIT License 5 votes vote down vote up
def call_in_thread(self, callback):
        """Call the given function on a thread.

        Args:
            callback (:obj:`callable`): Callable function to be invoked in a thread.
        """
        reactor.callInThread(callback) 
Example #18
Source File: test_threads.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testCallFromThread(self):
        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) 
Example #19
Source File: test_threads.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testCallInThread(self):
        waiter = threading.Event()
        result = []
        def threadedFunc():
            result.append(threadable.isInIOThread())
            waiter.set()

        reactor.callInThread(threadedFunc)
        waiter.wait(120)
        if not waiter.isSet():
            self.fail("Timed out waiting for event.")
        else:
            self.assertEquals(result, [False]) 
Example #20
Source File: test_internet.py    From BitTorrent with GNU General Public License v3.0 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 #21
Source File: bzr_buildbot.py    From buildbot-contrib with GNU General Public License v2.0 5 votes vote down vote up
def deferToThreadInReactor(reactor, f, *args, **kwargs):
    """
    Run function in thread and return result as Deferred.
    """
    d = defer.Deferred()
    reactor.callInThread(_putResultInDeferred, reactor, d, f, args, kwargs)
    return d

# uses its own reactor for the threaded calls, unlike Twisted's 
Example #22
Source File: s3_storage_provider.py    From synapse-s3-storage-provider with Apache License 2.0 5 votes vote down vote up
def store_file(self, path, file_info):
        """See StorageProvider.store_file"""

        def _store_file():
            session = boto3.session.Session()
            session.resource("s3", **self.api_kwargs).Bucket(self.bucket).upload_file(
                Filename=os.path.join(self.cache_directory, path),
                Key=path,
                ExtraArgs={"StorageClass": self.storage_class},
            )

        # XXX: reactor.callInThread doesn't return anything, so I don't think this does
        # what the author intended.
        return make_deferred_yieldable(reactor.callInThread(_store_file)) 
Example #23
Source File: s3_storage_provider.py    From synapse-s3-storage-provider with Apache License 2.0 5 votes vote down vote up
def fetch(self, path, file_info):
        """See StorageProvider.fetch"""
        logcontext = current_context()

        d = defer.Deferred()
        self._download_pool.callInThread(
            s3_download_task, self.bucket, self.api_kwargs, path, d, logcontext
        )
        return make_deferred_yieldable(d) 
Example #24
Source File: newmovies.ugo.py    From hatena-server with GNU Affero General Public License v3.0 5 votes vote down vote up
def Update(self):#called every minute
		reactor.callLater(60, self.Update)
		
		if self.newestflip <> Database.Newest[0]:
			self.newestflip = Database.Newest[0]
			reactor.callInThread(self.UpdateThreaded, Database.Newest) 
Example #25
Source File: likedmovies.ugo.py    From hatena-server with GNU Affero General Public License v3.0 5 votes vote down vote up
def Update(self):#called every 15 minutes
		reactor.callLater(60*10, self.Update)
		
		if self.newestflip <> Database.Newest[0] or self.neweststar <> Database.Stars:
			self.newestflip = Database.Newest[0]
			self.neweststar = Database.Stars
			reactor.callInThread(self.UpdateThreaded, Database.Newest) 
Example #26
Source File: hotmovies.ugo.py    From hatena-server with GNU Affero General Public License v3.0 5 votes vote down vote up
def Update(self):#called every 15 minutes
		reactor.callLater(60*10, self.Update)
		
		if self.newestflip <> Database.Newest[0] or self.newestview <> Database.Views:
			self.newestflip = Database.Newest[0]
			self.newestview = Database.Views
			reactor.callInThread(self.UpdateThreaded, Database.Newest) 
Example #27
Source File: iridium_protocol.py    From iridium with GNU General Public License v3.0 5 votes vote down vote up
def connectionMade(self):
        reactor.callInThread(self.configure)

        # Report connection has been made
        if self.cb_connected is not None:
            self.cb_connected(True) 
Example #28
Source File: iridium_protocol.py    From iridium with GNU General Public License v3.0 5 votes vote down vote up
def hangup(self):
        if not self.in_call:
            return True

        reactor.callInThread(self._hangup)

    # Hangup in thread 
Example #29
Source File: iridium_protocol.py    From iridium with GNU General Public License v3.0 5 votes vote down vote up
def handle_creg(self, data):
        reactor.callInThread(self._handle_creg, data)

    # Handle the 'CREG' message in thread 
Example #30
Source File: iridium_protocol.py    From iridium with GNU General Public License v3.0 5 votes vote down vote up
def handle_cpin(self, data):
        reactor.callInThread(self._handle_cpin, data)

    # Handle the 'CPIN' message in thread