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


Python threads.deferToThread方法代碼示例

本文整理匯總了Python中twisted.internet.threads.deferToThread方法的典型用法代碼示例。如果您正苦於以下問題:Python threads.deferToThread方法的具體用法?Python threads.deferToThread怎麽用?Python threads.deferToThread使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在twisted.internet.threads的用法示例。


在下文中一共展示了threads.deferToThread方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: connectionMade

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def connectionMade(self):
        self._sendSASLMessage(self.START, self.sasl.mechanism)
        initial_message = yield deferToThread(self.sasl.process)
        self._sendSASLMessage(self.OK, initial_message)

        while True:
            status, challenge = yield self._receiveSASLMessage()
            if status == self.OK:
                response = yield deferToThread(self.sasl.process, challenge)
                self._sendSASLMessage(self.OK, response)
            elif status == self.COMPLETE:
                if not self.sasl.complete:
                    msg = "The server erroneously indicated that SASL " \
                          "negotiation was complete"
                    raise TTransport.TTransportException(msg, message=msg)
                else:
                    break
            else:
                msg = "Bad SASL negotiation status: %d (%s)" % (status, challenge)
                raise TTransport.TTransportException(msg, message=msg)

        self._sasl_negotiation_deferred = None
        ThriftClientProtocol.connectionMade(self) 
開發者ID:XiaoMi,項目名稱:galaxy-sdk-python,代碼行數:25,代碼來源:TTwisted.py

示例2: test_http_json

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_http_json(machinery, create_influxdb, reset_influxdb):
    """
    Submit single reading in JSON format to HTTP API
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 25.26,
        'humidity': 51.8,
    }
    yield threads.deferToThread(http_json_sensor, settings.channel_path_data, data)

    # Wait for some time to process the message.
    yield sleep(PROCESS_DELAY)

    # Proof that data arrived in InfluxDB.
    record = influx_sensors.get_first_record()
    del record['time']
    assert record == {u'temperature': 25.26, u'humidity': 51.8}
    yield record 
開發者ID:daq-tools,項目名稱:kotori,代碼行數:23,代碼來源:test_daq_http.py

示例3: test_http_urlencoded

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_http_urlencoded(machinery, create_influxdb, reset_influxdb):
    """
    Submit single reading in ``x-www-form-urlencoded`` format to HTTP API
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 25.26,
        'humidity': 51.8,
    }
    yield threads.deferToThread(http_form_sensor, settings.channel_path_data, data)

    # Wait for some time to process the message.
    yield sleep(PROCESS_DELAY)

    # Proof that data arrived in InfluxDB.
    record = influx_sensors.get_first_record()
    del record['time']
    assert record == {u'temperature': 25.26, u'humidity': 51.8}
    yield record 
開發者ID:daq-tools,項目名稱:kotori,代碼行數:23,代碼來源:test_daq_http.py

示例4: test_http_csv

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_http_csv(machinery, create_influxdb, reset_influxdb):
    """
    Submit single reading in CSV format to HTTP API
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 25.26,
        'humidity': 51.8,
    }
    yield threads.deferToThread(http_csv_sensor, settings.channel_path_data, data)

    # Wait for some time to process the message.
    yield sleep(PROCESS_DELAY)

    # Proof that data arrived in InfluxDB.
    record = influx_sensors.get_first_record()
    del record['time']
    assert record == {u'temperature': 25.26, u'humidity': 51.8}
    yield record 
開發者ID:daq-tools,項目名稱:kotori,代碼行數:23,代碼來源:test_daq_http.py

示例5: test_weewx_mqtt

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_weewx_mqtt(machinery, create_influxdb, reset_influxdb):
    """
    Publish single reading in JSON format to MQTT broker
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    yield threads.deferToThread(mqtt_json_sensor, settings.mqtt_topic_json, data)

    # Wait for some time to process the message.
    yield sleep(PROCESS_DELAY)

    # Proof that data arrived in InfluxDB.
    record = influx_sensors.get_first_record()

    assert record["time"] == '2017-04-17T22:15:00Z'

    assert record["outTemp_C"] == 3.55555555556
    assert record["windSpeed10_kph"] == 5.78725803977
    assert record["cloudbase_meter"] == 773.082217509
    assert record["consBatteryVoltage_volt"] == 4.72

    yield record 
開發者ID:daq-tools,項目名稱:kotori,代碼行數:25,代碼來源:test_weewx.py

示例6: test_mqtt_legacy

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_mqtt_legacy(machinery, create_influxdb, reset_influxdb):
    """
    Publish single reading in JSON format to MQTT broker on legacy suffix
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 42.84,
        'humidity': 83.1,
    }
    yield threads.deferToThread(mqtt_json_sensor, settings.mqtt_topic_json_legacy, data)

    # Wait for some time to process the message.
    yield sleep(PROCESS_DELAY)

    # Proof that data arrived in InfluxDB.
    record = influx_sensors.get_first_record()
    assert 'temperature' in record or 'humidity' in record 
開發者ID:daq-tools,項目名稱:kotori,代碼行數:21,代碼來源:test_daq_mqtt.py

示例7: test_mqtt_to_influxdb_single

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_mqtt_to_influxdb_single(machinery, create_influxdb, reset_influxdb):
    """
    Publish discrete values to the MQTT broker and
    and proof they are stored in the InfluxDB database.
    """

    # Submit discrete measurement values, without timestamp.
    topic_temperature = settings.mqtt_topic_single + '/temperature'
    topic_humidity = settings.mqtt_topic_single + '/humidity'
    yield threads.deferToThread(mqtt_sensor, topic_temperature, 42.84)
    yield threads.deferToThread(mqtt_sensor, topic_humidity, 83.1)

    # Wait for some time to process the message.
    yield sleep(PROCESS_DELAY)

    # Proof that data arrived in InfluxDB.
    record = influx_sensors.get_first_record()
    assert 'temperature' in record or 'humidity' in record 
開發者ID:daq-tools,項目名稱:kotori,代碼行數:20,代碼來源:test_daq_mqtt.py

示例8: test_mqtt_homie

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_mqtt_homie(machinery, create_influxdb, reset_influxdb):
    """
    Publish reading like a Homie device in JSON format to MQTT broker
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 42.84,
        'humidity': 83.1,
    }
    yield threads.deferToThread(mqtt_json_sensor, settings.mqtt_topic_homie, data)

    # Wait for some time to process the message.
    yield sleep(PROCESS_DELAY)

    # Proof that data arrived in InfluxDB.
    record = influx_sensors.get_first_record()
    del record['time']
    assert record == {u'humidity': 83.1, u'temperature': 42.84}
    yield record 
開發者ID:daq-tools,項目名稱:kotori,代碼行數:23,代碼來源:test_homie.py

示例9: test_airrohr_http_json

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_airrohr_http_json(machinery, create_influxdb, reset_influxdb):
    """
    Submit single reading in Airrohr JSON format to HTTP API
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    yield threads.deferToThread(http_json_sensor, settings.channel_path_airrohr, data_in)

    # Wait for some time to process the message.
    yield sleep(PROCESS_DELAY)

    # Proof that data arrived in InfluxDB.
    record = influx_sensors.get_first_record()
    del record['time']
    assert record == data_out
    yield record 
開發者ID:daq-tools,項目名稱:kotori,代碼行數:19,代碼來源:test_airrohr.py

示例10: test_event_http_json

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_event_http_json(machinery, create_influxdb, reset_influxdb):
    """
    Submit event in JSON format to HTTP API
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single event, without timestamp.
    yield threads.deferToThread(http_json_sensor, settings.channel_path_event, event_data)

    # Wait for some time to process the message.
    yield sleep(PROCESS_DELAY)

    # Proof that event arrived in InfluxDB.
    record = influx_events.get_first_record()
    del record['time']
    assert record == event_data
    yield record 
開發者ID:daq-tools,項目名稱:kotori,代碼行數:19,代碼來源:test_daq_events.py

示例11: test_event_http_urlencoded

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_event_http_urlencoded(machinery, create_influxdb, reset_influxdb):
    """
    Submit event in ``x-www-form-urlencoded`` format to HTTP API
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single event, without timestamp.
    yield threads.deferToThread(http_form_sensor, settings.channel_path_event, event_data)

    # Wait for some time to process the message.
    yield sleep(PROCESS_DELAY)

    # Proof that event arrived in InfluxDB.
    record = influx_events.get_first_record()
    del record['time']
    assert record == event_data
    yield record 
開發者ID:daq-tools,項目名稱:kotori,代碼行數:19,代碼來源:test_daq_events.py

示例12: _read_handshake

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def _read_handshake(self, data):
        """
        Read handshake message, parse the other peer's public key and
        schedule the key exchange for execution outside of the event loop.
        """

        log_prefix = "obfs3:_read_handshake()"
        if len(data) < PUBKEY_LEN:
            log.debug("%s: Not enough bytes for key (%d)." % (log_prefix, len(data)))
            return

        log.debug("%s: Got %d bytes of handshake data (waiting for key)." % (log_prefix, len(data)))

        # Get the public key from the handshake message, do the DH and
        # get the shared secret.
        other_pubkey = data.read(PUBKEY_LEN)

        # Do the UniformDH handshake asynchronously
        self.d = threads.deferToThread(self.dh.get_secret, other_pubkey)
        self.d.addCallback(self._read_handshake_post_dh, other_pubkey, data)
        self.d.addErrback(self._uniform_dh_errback, other_pubkey)

        self.state = ST_WAIT_FOR_HANDSHAKE 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:25,代碼來源:obfs3.py

示例13: test_deferredFailureAfterSuccess

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_deferredFailureAfterSuccess(self):
        """
        Check that a successful L{threads.deferToThread} followed by a one
        that raises an exception correctly result as a failure.
        """
        # set up a condition that causes cReactor to hang. These conditions
        # can also be set by other tests when the full test suite is run in
        # alphabetical order (test_flow.FlowTest.testThreaded followed by
        # test_internet.ReactorCoreTestCase.testStop, to be precise). By
        # setting them up explicitly here, we can reproduce the hang in a
        # single precise test case instead of depending upon side effects of
        # other tests.
        #
        # alas, this test appears to flunk the default reactor too

        d = threads.deferToThread(lambda: None)
        d.addCallback(lambda ign: threads.deferToThread(lambda: 1//0))
        return self.assertFailure(d, ZeroDivisionError) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:20,代碼來源:test_threads.py

示例14: test_runs_command

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_runs_command(self):
        """
        ``run()`` on a SSH ``ProcessNode`` runs the command on the machine
        being ssh'd into.
        """
        node = make_sshnode(self)
        temp_file = FilePath(self.mktemp())

        def go():
            with node.run([b"python", b"-c",
                           b"file('%s', 'w').write(b'hello')"
                           % (temp_file.path,)]):
                pass
            return temp_file.getContent()
        d = deferToThread(go)

        def got_data(data):
            self.assertEqual(data, b"hello")
        d.addCallback(got_data)
        return d 
開發者ID:ClusterHQ,項目名稱:flocker,代碼行數:22,代碼來源:test_ipc.py

示例15: test_run_stdin

# 需要導入模塊: from twisted.internet import threads [as 別名]
# 或者: from twisted.internet.threads import deferToThread [as 別名]
def test_run_stdin(self):
        """
        ``run()`` on a SSH ``ProcessNode`` writes to the remote command's
        stdin.
        """
        node = make_sshnode(self)
        temp_file = FilePath(self.mktemp())

        def go():
            with node.run([b"python", b"-c",
                           b"import sys; "
                           b"file('%s', 'wb').write(sys.stdin.read())"
                           % (temp_file.path,)]) as stdin:
                stdin.write(b"hello ")
                stdin.write(b"there")
            return temp_file.getContent()
        d = deferToThread(go)

        def got_data(data):
            self.assertEqual(data, b"hello there")
        d.addCallback(got_data)
        return d 
開發者ID:ClusterHQ,項目名稱:flocker,代碼行數:24,代碼來源:test_ipc.py


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