twisted.internet.reactor.callFromThread

Here are the examples of the python api twisted.internet.reactor.callFromThread taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

142 Examples 7

Example 1

Project: canvas Source File: server.py
    def startService(self):
        internet.TCPServer.startService(self)
        self.update_nginx()
        signal.signal(signal.SIGUSR1, lambda signum, frame: reactor.callFromThread(self.on_SIGUSR1))
        port = reactor.listenTCP(0, CanvasManholeFactory())
        file('manhole.port', 'w').write(str(port.getHost().port))

Example 2

Project: calvin-base Source File: test_calvin_transport.py
    def _read_thread(self):
        print("%s - Read thread started" % self._name)
        while self._running:
            try:
                cmd = self._inqueue.get(timeout=.1)
            except:
                continue
            func = getattr(self, cmd[0])
            print("Running: %s(%s, %s)" % (func.__name__, cmd[1], cmd[2]))
            reactor.callFromThread(func, *cmd[1], **cmd[2])
        print("%s - Read thread died" % self._name)

Example 3

Project: txTelegramBot Source File: twistedclient.py
    def _poll_updates_thread(self):
        m = getUpdates()
        m.timeout = self._timeout
        m.limit = self._limit
        if self._offset is not None:
            m.offset = self._offset
        try:
            updates = self._client.send_method(m)
            reactor.callFromThread(self._handle_updates, updates)
        except Exception as e:
            reactor.callFromThread(self._handle_updates_error, e)

Example 4

Project: flumotion Source File: integration.py
Function: start
    def start(self):
        assert self.state == self.NOT_STARTED
        info('spawning thread %r, argv=%r', self, self.argv)
        self.protocol = ProcessProtocol()
        reactor.callFromThread(self.method_wrapper)

        self.state = self.STARTED

        def got_exit(res):
            self.state = self.STOPPED
            info('process %r has stopped', self)
            return res
        self.protocol.getDeferred().addCallback(got_exit)

Example 5

Project: flumotion Source File: disker.py
    def _client_removed_cb(self, element, arg0, client_status):
        # treat as error if we were removed because of GST_CLIENT_STATUS_ERROR
        # FIXME: can we use the symbol instead of a numeric constant ?
        if client_status == 4:
            # since we get called from the streaming thread, hand off handling
            # to the reactor's thread
            reactor.callFromThread(self._client_error_cb)

        if self.writeIndex:
            del self._clients[arg0]

Example 6

Project: b2bua Source File: Signal.py
Function: signal_handler
    def signal_handler(self, signum, *frame):
        reactor.callFromThread(self.dispatch)
        if self.previous_handler not in (SIG_IGN, SIG_DFL):
            try:
                self.previous_handler(signum, *frame)
            except:
                print datetime.now(), 'Signal: unhandled exception in signal chain'
                print '-' * 70
                print_exc(file = stdout)
                print '-' * 70
                stdout.flush()

Example 7

Project: python-driver Source File: twistedreactor.py
Function: push
    def push(self, data):
        """
        This function is called when outgoing data should be queued
        for sending.

        Note that we can't call transport.write() directly because
        it is not thread-safe, so we schedule it to run from within
        the event loop when it gets the chance.
        """
        reactor.callFromThread(self.connector.transport.write, data)

Example 8

Project: scrapy Source File: crawler.py
    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)

Example 9

Project: gridsync Source File: sync.py
Function: on_modified
    def on_modified(self, event):
        self.filesystem_modified = True
        #try:
        #    reactor.callFromThread(self.local_checker.start, 1)
        #except AssertionError:
        #    return
        reactor.callFromThread(self._start_local_checker)

Example 10

Project: scrapy Source File: test_downloadermiddleware_robotstxt.py
    def _get_successful_crawler(self):
        crawler = self.crawler
        crawler.settings.set('ROBOTSTXT_OBEY', True)
        ROBOTS = re.sub(b'^\s+(?m)', b'', b'''
        User-Agent: *
        Disallow: /admin/
        Disallow: /static/
        ''')
        response = TextResponse('http://site.local/robots.txt', body=ROBOTS)
        def return_response(request, spider):
            deferred = Deferred()
            reactor.callFromThread(deferred.callback, response)
            return deferred
        crawler.engine.download.side_effect = return_response
        return crawler

Example 11

Project: flumotion Source File: cachedprovider.py
Function: do_serve
    def doServe(self):
        if not (self.copying and self._pending):
            # Nothing to do anymore.
            return False
        # We have pending source file read operations
        position, size, d = self._pending.pop(0)
        self._sourceFile.seek(position)
        data = self._sourceFile.read(size)
        # Call the deferred in the main thread
        reactor.callFromThread(d.callback, data)
        return len(self._pending) > 0

Example 12

Project: flumotion Source File: icystreamer.py
Function: tag_event_cb
    def _tag_event_cb(self, pad, event):

        def store_tag(struc, headerKey, structureKey):
            if structureKey in struc.keys():
                self.icyHeaders[headerKey] = struc[structureKey]
                self.debug("Set header key %s = %s", \
                        headerKey, struc[structureKey])

        mapping = {'icy-name': 'organization',
                   'icy-genre': 'genre',
                   'icy-url': 'location'}
        if event.type == gst.EVENT_TAG:
            struc = event.get_structure()
            self.debug('Structure keys of tag event: %r', struc.keys())
            for headerName in mapping:
                reactor.callFromThread(\
                        store_tag, struc, headerName, mapping[headerName])
        return True

Example 13

Project: scrapy Source File: test_downloadermiddleware_robotstxt.py
    def test_ignore_robotstxt_request(self):
        self.crawler.settings.set('ROBOTSTXT_OBEY', True)
        def ignore_request(request, spider):
            deferred = Deferred()
            reactor.callFromThread(deferred.errback, failure.Failure(IgnoreRequest()))
            return deferred
        self.crawler.engine.download.side_effect = ignore_request

        middleware = RobotsTxtMiddleware(self.crawler)
        mw_module_logger.error = mock.MagicMock()

        d = self.assertNotIgnored(Request('http://site.local/allowed'), middleware)
        d.addCallback(lambda _: self.assertFalse(mw_module_logger.error.called))
        return d

Example 14

Project: PiIO.WS Source File: server_protocol.py
    def register_rpi(self, rpi):
        # we need mac, ip, interface desc
        payload = {}
        payload['mac'] = rpi.mac
        payload['ip'] = rpi.protocol.peer.host
        payload['iface'] = rpi.iface

        post_data = {'json':json.dumps(payload)}
        post_data = urllib.urlencode(post_data)
        try:
            url = urllib2.Request('http://%s/ws_comm/register/' % settings.SITE_SERVER_ADDRESS, post_data)
            url_response = urllib2.urlopen(url)
            url_response.read()
        except:
            pass
        # TODO: success validation

        # notify users
        reactor.callFromThread(self.ws_factory.register_rpi_wsite, rpi)

Example 15

Project: gazouilleur Source File: feeds.py
Function: init
    def __init__(self, factory):
        self.fact = factory
        self.threadpool = ThreadPool(1,25)
        reactor.callFromThread(self.threadpool.start)
        # Allow Ctrl-C to get you out cleanly:
        reactor.addSystemEventTrigger('after', 'shutdown', self.threadpool.stop)
        self.pile = []
        self.depiler = None
        self.depiler_running = False

Example 16

Project: greplin-twisted-utils Source File: twistlet.py
Function: run_one
def _runOne(fn, args, kw, out, queue):
  """Runs a single eventlet task."""
  try:
    result = fn(*args, **kw)
    if out:
      reactor.callFromThread(out.callback, result)
    else:
      queue.put(result)
  except: # OK to be generic here since basically re-throw. # pylint: disable=W0702
    f = failure.Failure()
    if out:
      reactor.callFromThread(out.errback, f)
    else:
      queue.put(f)

Example 17

Project: yabgp Source File: protocol.py
    def send_update(self, msg):
        """
        send update message to the peer
        :param msg: message dictionary
        :return:
        """
        try:
            msg_update = Update().construct(msg, self.fourbytesas, self.add_path_ipv4_send)
            reactor.callFromThread(self.write_tcp_thread, msg_update)
            self.msg_sent_stat['Updates'] += 1
            return True
        except Exception as e:
            LOG.error(e)
            return False

Example 18

Project: filesync-server Source File: notifier.py
Function: on_event
    def on_event(self, event):
        """An event was received, call the proper callback if any."""
        cback = self._event_callbacks.get(event.event_type)
        if cback is not None:
            if event.call_with_recipient:
                for recipient_id in event.recipient_ids:
                    reactor.callFromThread(cback, event,
                                           recipient_id=recipient_id)
            else:
                reactor.callFromThread(cback, event)

Example 19

Project: TALOS Source File: phantom.py
Function: data_received
	def dataReceived(self, data):
		try:
			out = self.parse_com(data)
		except Exception, e:
			print "Unexpected error:", sys.exc_info()[0]
			print e
			out = "print phantom exception %s" % e
			traceback.print_exc(file=open("/tmp/errlog.txt","a"))
			
		if out is not None:
			reactor.callFromThread(self.transport.write, out)
			return
		else:
			reactor.callFromThread(self.transport.write, "readyForCommand")
			return

Example 20

Project: Piped Source File: db.py
Function: cursor_execute
    def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
        try:
            return execute(cursor, statement, parameters, context)
        except self.intercepted_exceptions:
            reactor.callFromThread(self.manager.on_connection_failed, failure.Failure())
            raise

Example 21

Project: flumotion Source File: fgdp.py
    def _check_eos(self, pad, event):
        if event.type == gst.EVENT_EOS:
            # EOS are triggered after a disconnection, when the read in the
            # socket is 0 Bytes. Remove the handler and close the connection
            pad.remove_event_probe(self._handler_id)
            if self.protocol is not None:
                reactor.callFromThread(self.protocol.loseConnection)
            return False
        return True

Example 22

Project: flumotion Source File: multifdsinkstreamer.py
Function: notify_caps_cb
    def _notify_caps_cb(self, element, pad, param):
        # We store caps in sink objects as
        # each sink might (and will) serve different content-type
        caps = pad.get_negotiated_caps()
        if caps == None:
            return

        caps_str = gstreamer.caps_repr(caps)
        self.debug('Got caps: %s' % caps_str)

        if not element.caps == None:
            self.warning('Already had caps: %s, replacing' % caps_str)

        self.debug('Storing caps: %s' % caps_str)
        element.caps = caps

        reactor.callFromThread(self.update_ui_state)

Example 23

Project: pulp Source File: server.py
Function: write
    def write(self, data):
        """
        Forward the data to the request.write method, which writes data to
        the transport (if not responding to a HEAD request).

        :param data: A string to write to the response.
        :type  data: str
        """
        reactor.callFromThread(self.request.write, data)

Example 24

Project: flumotion Source File: hlsstreamer.py
    def _new_fragment(self, hlssink):
        self.log("hlsink created a new fragment")
        try:
            fragment = hlssink.get_property('fragment')
        except:
            fragment = hlssink.emit('pull-fragment')
        reactor.callFromThread(self._process_fragment, fragment)

Example 25

Project: scrapy Source File: test_downloadermiddleware_robotstxt.py
    def _get_garbage_crawler(self):
        crawler = self.crawler
        crawler.settings.set('ROBOTSTXT_OBEY', True)
        response = Response('http://site.local/robots.txt', body=b'GIF89a\xd3\x00\xfe\x00\xa2')
        def return_response(request, spider):
            deferred = Deferred()
            reactor.callFromThread(deferred.callback, response)
            return deferred
        crawler.engine.download.side_effect = return_response
        return crawler

Example 26

Project: flumotion Source File: feeder.py
Function: disconnected
    def disconnected(self, when=None, fd=None):
        """
        The client has disconnected.
        Update related stats.

        Called from GStreamer threads.
        """
        if self.fd != fd:
            # assume that connected() already called
            # _updateUIStateForDisconnect for us
            return

        if not when:
            when = time.time()

        reactor.callFromThread(self._updateUIStateForDisconnect, fd,
                               when)

Example 27

Project: flumotion Source File: eater.py
Function: disconnected
    def disconnected(self, when=None):
        """
        The eater has been disconnected.
        Update related stats.
        """
        if not when:
            when = time.time()

        def updateUIState():
            self.uiState.set('last-disconnect', when)
            self.fd = None
            self.uiState.set('fd', None)

        reactor.callFromThread(updateUIState)

Example 28

Project: scrapy Source File: test_downloadermiddleware_robotstxt.py
    def test_robotstxt_error(self):
        self.crawler.settings.set('ROBOTSTXT_OBEY', True)
        err = error.DNSLookupError('Robotstxt address not found')
        def return_failure(request, spider):
            deferred = Deferred()
            reactor.callFromThread(deferred.errback, failure.Failure(err))
            return deferred
        self.crawler.engine.download.side_effect = return_failure

        middleware = RobotsTxtMiddleware(self.crawler)
        middleware._logerror = mock.MagicMock(side_effect=middleware._logerror)
        deferred = middleware.process_request(Request('http://site.local'), None)
        deferred.addCallback(lambda _: self.assertTrue(middleware._logerror.called))
        return deferred

Example 29

Project: flumotion Source File: test_pbstream.py
    def _handoff_cb(self, sink, buffer, pad):
        # called once, and only once
        self.debug("buffer received, stopping")
        sink.disconnect(self._id)
        self._id = None
        # we get executed from the streaming thread, so we want to
        # callback our deferred from the reactor thread
        reactor.callFromThread(self.callback, "handoff")

Example 30

Project: ripestat-text Source File: server.py
    def queueLine(self, line):
        """
        Callback method to allow StatCore to send output over the network.

        Each line is queued in the main thread for sending.
        """
        reactor.callFromThread(self.sendLine, line.encode("utf-8"))

Example 31

Project: Ultros Source File: resolver.py
Function: get_host_by_name
    def get_host_by_name(self, address):
        d = defer.Deferred()

        def func():
            try:
                reactor.callFromThread(
                    d.callback, socket.gethostbyname(address)
                )
            except Exception as e:
                reactor.callFromThread(d.errback, e)

        self.pool.callInThread(func)
        return d

Example 32

Project: b2bua Source File: Rtp_proxy_session.py
    def __del__(self):
        if self.my_ident != get_ident():
            #print 'Rtp_proxy_session.__del__() from wrong thread, re-routing'
            reactor.callFromThread(self.delete)
        else:
            self.delete()

Example 33

Project: PiIO.WS Source File: server_protocol.py
    def disconnect_rpi(self, rpi):
        payload = {}
        payload['mac'] = rpi.mac

        post_data = {'json':json.dumps(payload)}
        post_data = urllib.urlencode(post_data)
        try:
            url = urllib2.Request('http://%s/ws_comm/disconnect/' % settings.SITE_SERVER_ADDRESS, post_data)
            url_response = urllib2.urlopen(url)
            url_response.read()
        except:
            pass

        # notify users
        reactor.callFromThread(self.ws_factory.disconnect_rpi_wsite, rpi)

Example 34

Project: TALOS Source File: phantom.py
Function: tripcode
	def tripcode(self, tripcode=None):
		#self.transport.write("tripcode %s" % tripcode)
		#reactor.callFromThread(self.transport.write, "tripcode %s" % tripcode)
		#print "tripcode function launched"
		if tripcode is not None:
            	#	print "MADE IT"
			reactor.callFromThread(self.transport.write, "tripcode %s" % tripcode)
        	#deferToThread(self.q.get).addCallback(self.tripcode)
		return True

Example 35

Project: crossbar Source File: fswatcher.py
Function: start
        def start(self, callback):
            """
            Start watching.
            """
            if not self._started:
                def on_any_event(evt):
                    event = {
                        u'type': evt.event_type,
                        u'abs_path': os.path.abspath(evt.src_path),
                        u'rel_path': os.path.relpath(evt.src_path, self._working_dir),
                        u'is_directory': evt.is_directory,
                    }
                    reactor.callFromThread(callback, event)

                self._handler.on_any_event = on_any_event
                self._observer.start()

Example 36

Project: b2bua Source File: XMPP_server.py
    def on_stanza(self, doc):
        if doc.name() == 'incoming_packet':
            data = base64.b64decode(doc.get('msg'))
            raddr = (doc.get('src_addr'), int(doc.get('src_port')))
            laddr = (doc.get('dst_addr'), int(doc.get('dst_port')))
            rtime = float(doc.get('rtime'))
            reactor.callFromThread(self.__owner.handle_read, data, raddr, laddr, rtime)

Example 37

Project: python-driver Source File: twistedreactor.py
Function: init
    def __init__(self, *args, **kwargs):
        """
        Initialization method.

        Note that we can't call reactor methods directly here because
        it's not thread-safe, so we schedule the reactor/connection
        stuff to be run from the event loop thread when it gets the
        chance.
        """
        Connection.__init__(self, *args, **kwargs)

        self.is_closed = True
        self.connector = None

        reactor.callFromThread(self.add_connection)
        self._loop.maybe_start()

Example 38

Project: autobahn-sync Source File: callbacks_runner.py
Function: start
    def start(self):
        assert not self._started, 'Already started !'
        self._started = True
        while self._started:
            # Get back and execute requested callbacks
            func, answer = self._callbacks.get()
            try:
                reactor.callFromThread(answer.callback, func())
            except Exception as e:
                reactor.callFromThread(answer.errback, e)

Example 39

Project: filesync-server Source File: cmd_client.py
Function: do_disconnect
    @parse_args()
    def do_disconnect(self):
        """Disconnect."""
        if self.status != "connected":
            print "ERROR: Not connecting."
            return
        reactor.callFromThread(
            self.factory.current_protocol.transport.loseConnection)

Example 40

Project: smap Source File: core.py
Function: add
    def add(self, *args):
        """A version of :py:meth:`~Timeseries._add` which can be called from any thread.
        """
        # SDH : thread-safe
        # this way the real add is always done in the main loop,
        # even if it was called by another threadpool or something.
        reactor.callFromThread(lambda: self._add(*args))

Example 41

Project: python-sysfs-gpio Source File: gpio.py
    def _poll_queue_loop(self):

        while self._running:
            events = self._poll_queue.poll(EPOLL_TIMEOUT)
            if len(events) > 0:
                reactor.callFromThread(self._poll_queue_event, events)

Example 42

Project: hellanzb Source File: Logging.py
    def scrollHeader(self, record):
        """ Print a log message so that the user can see it during a SCROLL """
        msg = self.format(record).rstrip() # Scroller appends newline for us
        from twisted.internet import reactor
        if inMainThread():
            # FIXME: scrollBegin() should really be creating the scroller instance
            # FIXME: no unicode crap from normal python log emit
            Hellanzb.scroller.scrollHeader(msg)
        else:
            reactor.callFromThread(Hellanzb.scroller.scrollHeader, msg)

Example 43

Project: feat Source File: resources.py
    def _write_resource(self, response, res):

        try:
            while True:
                data = res.read(self.BUFFER_SIZE)
                if not data:
                    break
                reactor.callFromThread(response.write, #@UndefinedVariable
                                       data)
        finally:
            res.close()

Example 44

Project: freepy Source File: http.py
  def _dispatch(self, message):
    for rule in self.__rules__:
      target = rule.get('target')
      urls = rule.get('urls')
      for url in urls:
        result = re.match(url, message.path)
        if result is not None:
          qs = self._parse_qs(message)
          event = HttpRequestEvent(result.groups(),
                                   result.groupdict(),
                                   qs,
                                   message)
          self.__server__.tell(RouteMessageCommand(event, target))
          return
    deferred = reactor.callFromThread(self._dispatch_not_found, message)

Example 45

Project: scrapy Source File: crawler.py
    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 46

Project: pixelated-user-agent Source File: sessions.py
    @defer.inlineCallbacks
    def finish_bootstrap(self):
        yield self.keymanager.generate_openpgp_key()
        yield self._create_account(self.soledad, self.user_auth.uuid)
        self.incoming_mail_fetcher = yield self._create_incoming_mail_fetcher(
            self.keymanager,
            self.soledad,
            self.account,
            self.account_email())
        reactor.callFromThread(self.incoming_mail_fetcher.startService)

Example 47

Project: hellanzb Source File: ArticleDecoder.py
def tryFinishNZB(nzb):
    """ Determine if the NZB download/decode process is done for the specified NZB -- if it's
    done, trigger handleNZBDone. We'll call this check everytime we finish processing an
    nzbFile """
    #start = time.time()

    # Check if there are any more nzbFiles in the queue that belong to this nzb
    done = Hellanzb.queue.isNZBDone(nzb)
    if done:
        Hellanzb.queue.nzbDone(nzb)
        debug('tryFinishNZB: finished downloading NZB: ' + nzb.archiveName)
        
        reactor.callFromThread(handleNZBDone, nzb)
        
    #finish = time.time() - start
    #debug('tryFinishNZB (' + str(done) + ') took: ' + str(finish) + ' seconds')
    return done

Example 48

Project: ripestat-text Source File: server.py
    def processLinesDone(self, success, result):
        if not success:
            self.queueLine("There was an error processing this request. "
                           "Bugs can be reported to [email protected].")
            reactor.callFromThread(log.err, result)

        # Maintain or end the connection depending on the mode
        if self.keep_alive:
            reactor.callFromThread(reactor.addReader, self.reader)
        else:
            reactor.callFromThread(self.transport.loseConnection)

Example 49

Project: scrapy Source File: test_downloadermiddleware_robotstxt.py
    def _get_emptybody_crawler(self):
        crawler = self.crawler
        crawler.settings.set('ROBOTSTXT_OBEY', True)
        response = Response('http://site.local/robots.txt')
        def return_response(request, spider):
            deferred = Deferred()
            reactor.callFromThread(deferred.callback, response)
            return deferred
        crawler.engine.download.side_effect = return_response
        return crawler

Example 50

Project: flumotion Source File: feeder.py
    def clientDisconnected(self, fd):
        """
        The client has been entirely removed from multifdsink, and we may
        now close its file descriptor.
        The client object stays around so we can track over multiple
        connections.

        Called from GStreamer threads.

        @type fd: file descriptor
        """
        (client, cleanup) = self._fdToClient.pop(fd)
        client.disconnected(fd=fd)

        # To avoid races between this thread (a GStreamer thread) closing the
        # FD, and the reactor thread reusing this FD, we only actually perform
        # the close in the reactor thread.
        reactor.callFromThread(cleanup, fd)
See More Examples - Go to Next Page
Page 1 Selected Page 2 Page 3