Python twisted.internet.defer 模块,DeferredQueue() 实例源码

我们从Python开源项目中,提取了以下31个代码示例,用于说明如何使用twisted.internet.defer.DeferredQueue()

项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def __init__(self, domain, username, pw, server, use_ssl, policy_key=0, server_version="14.0", device_type="iPhone", device_id=None, verbose=False):
        self.use_ssl = use_ssl
        self.domain = domain
        self.username = username
        self.password = pw
        self.server = server
        self.device_id = device_id
        if not self.device_id:
            self.device_id = str(uuid.uuid4()).replace("-","")[:32]
        self.server_version = server_version
        self.device_type = device_type
        self.policy_key = policy_key
        self.folder_data = {}
        self.verbose = verbose
        self.collection_data = {}
        clientContext = WebClientContextFactory()
        self.agent = Agent(reactor, clientContext)
        self.operation_queue = defer.DeferredQueue()
        self.queue_deferred = self.operation_queue.get()
        self.queue_deferred.addCallback(self.queue_full)

    # Response processing
项目:TigerHost    作者:naphatkrit    | 项目源码 | 文件源码
def __init__(self, server_queue, client_queue):
        """Create a new protocol.

        :code:`server_queue` and :code:`client_queue` corresponds to the variables
        in the TCP proxy.

        :code:`self.wait_queue` is used to handle the race condition where
        :code:`self.client_queue` is ready to be consumed, but the connection has
        not been established.

        :param DeferredQueue server_queue:
        :param DeferredQueue client_queue:
        """
        self.server_queue = server_queue
        self.client_queue = client_queue
        self.wait_queue = DeferredQueue()
        self.client_queue.get().addCallback(self.clientQueueCallback)
项目:TigerHost    作者:naphatkrit    | 项目源码 | 文件源码
def __init__(self, spoof_hostname, spoof_port):
        """Create a new spoof TCP proxy.

        :param str spoof_hostname: the hostname of the spoof server
        :param int spoof_port: the port of the spoof server
        """
        # how many bytes have been sent by the spoof server?
        self.spoof_messages_length = 0

        # for the actual server connection
        self.server_queue = DeferredQueue()
        self.client_queue = DeferredQueue()

        # for the spoofed connection
        self.spoof_client_queue = DeferredQueue()
        self.spoof_server_queue = DeferredQueue()
        self._connectServer(spoof_hostname, spoof_port,
                            self.spoof_server_queue, self.spoof_client_queue)

        # add callbacks
        self.server_queue.get().addCallback(self.serverQueueCallback)
        self.spoof_server_queue.get().addCallback(self.spoofServerQueueCallback)
项目:TigerHost    作者:naphatkrit    | 项目源码 | 文件源码
def connectServer(self, hostname, port):
        """Tell the proxy what the end server is and start the connection. This closes the connection to the spoofed
        server.

        :param str hostname:
        :param int port:
        :param DeferredQueue server_queue:
        :param DeferredQueue client_queue:
        """
        # close connection
        spoof_client_queue = self.spoof_client_queue

        # setting to None first prevents a race condition
        self.spoof_client_queue = None
        self.spoof_server_queue = None
        spoof_client_queue.put(False)

        self._connectServer(
            hostname, port, self.server_queue, self.client_queue)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def test_deferred_queue_receiver(self):

        ebc = EventBus()

        queue = DeferredQueue()

        ebc.subscribe('', lambda _, msg: queue.put(msg))

        for i in xrange(10):
            ebc.publish('', i)

        self.assertEqual(len(queue.pending), 10)
        for i in xrange(10):
            msg = yield queue.get()
            self.assertEqual(msg, i)
        self.assertEqual(len(queue.pending), 0)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self, handler, adapter, device_id):
        self.log = structlog.get_logger(device_id=device_id)
        self._handler = handler
        self._adapter = adapter
        self._device_id = device_id
        self._proxy_address = None
        self._tx_tid = 1
        self._deferred = None         # TODO: Remove later if never used
        self._enabled = False
        self._requests = dict()       # Tx ID -> (timestamp, deferred, tx_frame, timeout)
        self._onu_messages = DeferredQueue(size=_MAX_INCOMING_OMCI_MESSAGES)

        # Statistics
        self._tx_frames = 0
        self._rx_frames = 0
        self._rx_onu_frames = 0       # Autonomously generated ONU frames
        self._rx_timeouts = 0
        self._tx_errors = 0           # Exceptions during tx request
        self._consecutive_errors = 0  # Rx & Tx errors in a row, good rx resets this to 0
        self._reply_min = sys.maxint  # Fastest successful tx -> rx
        self._reply_max = 0           # Longest successful tx -> rx
        self._reply_sum = 0.0         # Total seconds for successful tx->rx (float for average)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self, adapter, device_id):
        self.adapter = adapter
        self.adapter_agent = adapter.adapter_agent
        self.device_id = device_id
        self.log = structlog.get_logger(device_id=device_id)
        self.io_port = None
        self.logical_device_id = None
        self.interface = registry('main').get_args().interface
        self.pbc_factory = MaplePBClientFactory()
        self.pbc_port = 24498
        self.tx_id = 0
        self.onu_discovered_queue = DeferredQueue()
        self.rx_handler = MapleOltRxHandler(self.device_id, self.adapter, self.onu_discovered_queue)
        self.heartbeat_count = 0
        self.heartbeat_miss = 0
        self.heartbeat_interval = 1
        self.heartbeat_failed_limit = 3
        self.command_timeout = 5
        self.pm_metrics = None
        self.onus = {}
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self, adapter, device_id):
        self.adapter = adapter
        self.adapter_agent = adapter.adapter_agent
        self.device_id = device_id
        self.log = structlog.get_logger(device_id=device_id)
        self.incoming_messages = DeferredQueue()
        self.event_messages = DeferredQueue()
        self.proxy_address = None
        self.tx_id = 0

        # Need to query ONU for number of supported uni ports
        # For now, temporarily set number of ports to 1 - port #2
        self.uni_ports = (2,)

        # Handle received ONU event messages
        reactor.callLater(0, self.handle_onu_events)
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def test_cancelQueueAfterGet(self):
        """
        When canceling a L{Deferred} from a L{DeferredQueue} that does not
        have a result (i.e., the L{Deferred} has not fired), the cancel
        causes a L{defer.CancelledError} failure. If the queue has a result
        later on, it doesn't try to fire the deferred.
        """
        queue = defer.DeferredQueue()
        d = queue.get()
        d.cancel()
        self.assertImmediateFailure(d, defer.CancelledError)
        def cb(ignore):
            # If the deferred is still linked with the deferred queue, it will
            # fail with an AlreadyCalledError
            queue.put(None)
            return queue.get().addCallback(self.assertIs, None)
        d.addCallback(cb)
        done = []
        d.addCallback(done.append)
        self.assertEqual(len(done), 1)
项目:marathon-acme    作者:praekeltfoundation    | 项目源码 | 文件源码
def setUp(self):
        super(TestHTTPClientBase, self).setUp()

        self.requests = DeferredQueue()
        self.fake_server = FakeHttpServer(self.handle_request)

        fake_client = treq_HTTPClient(self.fake_server.get_agent())
        self.client = self.get_client(fake_client)

        # Spin the reactor once at the end of each test to clean up any
        # cancelled deferreds
        self.addCleanup(wait0)
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def testQueue(self):
        N, M = 2, 2
        queue = defer.DeferredQueue(N, M)

        gotten = []

        for i in range(M):
            queue.get().addCallback(gotten.append)
        self.assertRaises(defer.QueueUnderflow, queue.get)

        for i in range(M):
            queue.put(i)
            self.assertEquals(gotten, range(i + 1))
        for i in range(N):
            queue.put(N + i)
            self.assertEquals(gotten, range(M))
        self.assertRaises(defer.QueueOverflow, queue.put, None)

        gotten = []
        for i in range(N):
            queue.get().addCallback(gotten.append)
            self.assertEquals(gotten, range(N, N + i + 1))

        queue = defer.DeferredQueue()
        gotten = []
        for i in range(N):
            queue.get().addCallback(gotten.append)
        for i in range(N):
            queue.put(i)
        self.assertEquals(gotten, range(N))

        queue = defer.DeferredQueue(size=0)
        self.assertRaises(defer.QueueOverflow, queue.put, None)

        queue = defer.DeferredQueue(backlog=0)
        self.assertRaises(defer.QueueUnderflow, queue.get)
项目:flowder    作者:amir-khakshour    | 项目源码 | 文件源码
def __init__(self, app, poll_size=5):
        self.app = app
        self.dq = DeferredQueue(size=poll_size)
        self.queue = None
项目:relaax    作者:deeplearninc    | 项目源码 | 文件源码
def __init__(self, ws, client_id, srv_queue):
        self.ws = ws
        self.client_id = client_id
        self.cli_queue = defer.DeferredQueue()
        self.srv_queue = srv_queue
项目:relaax    作者:deeplearninc    | 项目源码 | 文件源码
def __init__(self):
        super(WebSocketServerProtocol, self).__init__()
        self.clients = {}
        self.srv_queue = defer.DeferredQueue()
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def testQueue(self):
        N, M = 2, 2
        queue = defer.DeferredQueue(N, M)

        gotten = []

        for i in range(M):
            queue.get().addCallback(gotten.append)
        self.assertRaises(defer.QueueUnderflow, queue.get)

        for i in range(M):
            queue.put(i)
            self.assertEquals(gotten, range(i + 1))
        for i in range(N):
            queue.put(N + i)
            self.assertEquals(gotten, range(M))
        self.assertRaises(defer.QueueOverflow, queue.put, None)

        gotten = []
        for i in range(N):
            queue.get().addCallback(gotten.append)
            self.assertEquals(gotten, range(N, N + i + 1))

        queue = defer.DeferredQueue()
        gotten = []
        for i in range(N):
            queue.get().addCallback(gotten.append)
        for i in range(N):
            queue.put(i)
        self.assertEquals(gotten, range(N))

        queue = defer.DeferredQueue(size=0)
        self.assertRaises(defer.QueueOverflow, queue.put, None)

        queue = defer.DeferredQueue(backlog=0)
        self.assertRaises(defer.QueueUnderflow, queue.get)
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def __init__(self, queue=None):
        if queue is None:
            queue = defer.DeferredQueue()
        self.queue = queue
项目:TigerHost    作者:naphatkrit    | 项目源码 | 文件源码
def client_queue():
    return DeferredQueue()
项目:TigerHost    作者:naphatkrit    | 项目源码 | 文件源码
def server_queue():
    return DeferredQueue()
项目:TigerHost    作者:naphatkrit    | 项目源码 | 文件源码
def __init__(self):
        """Create a new TCP proxy.

        `self.server_queue` contains messages from end server to client.
        `self.client_queue` contains messages from client to end server.
        """
        self.server_queue = DeferredQueue()
        self.client_queue = DeferredQueue()
        self.server_queue.get().addCallback(self.serverQueueCallback)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self, connection_manager, channel):

        self.connection_manager = connection_manager
        self.channel = channel
        self.local_stub = VolthaLocalServiceStub(channel)

        self.stopped = False

        self.packet_out_queue = Queue()  # queue to send out PacketOut msgs
        self.packet_in_queue = DeferredQueue()  # queue to receive PacketIn
        self.change_event_queue = DeferredQueue()  # queue change events
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self, adapter_agent, config):
        self.adapter_agent = adapter_agent
        self.config = config
        self.descriptor = Adapter(
            id=self.name,
            vendor='Tibit Communications Inc.',
            version='0.1',
            config=AdapterConfig(log_level=LogLevel.INFO)
        )
        self.interface = registry('main').get_args().interface
        self.io_port = None
        self.incoming_queues = {}  # OLT mac_address -> DeferredQueue()
        self.device_ids = {}  # OLT mac_address -> device_id
        self.vlan_to_device_ids = {}  # c-vid -> (device_id, logical_device_id, mac_address)
        self.mode = "GPON"
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def start(self):
        assert self._enabled, 'Start should only be called if enabled'
        #
        # TODO: Perform common startup tasks here
        #
        self._cancel_deferred()

        self._omci = OMCISupport(self, self.adapter, self.device_id)
        self._omci.enabled = True

        # Handle received ONU event messages
        self._event_messages = DeferredQueue()
        self._event_deferred = reactor.callLater(0, self._handle_onu_events)

        # Register for adapter messages
        self.adapter_agent.register_for_inter_adapter_messages()

        # Port startup
        for port in self.uni_ports:
            port.enabled = True

        for port in self.pon_ports:
            port.enabled = True

        # Heartbeat
        self._heartbeat.enabled = True
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self, device_id, adapter, onu_queue):
        self.device_id = device_id
        self.adapter = adapter
        self.onu_discovered_queue = onu_queue
        self.adapter_agent = adapter.adapter_agent
        self.adapter_name = adapter.name
        # registry('main').get_args().external_host_address
        self.pb_server_ip = '192.168.24.20'
        self.pb_server_port = 24497
        self.pb_server_factory = pb.PBServerFactory(self)
        # start PB server
        self.listen_port = reactor.listenTCP(self.pb_server_port,
                                             self.pb_server_factory)
        self.omci_rx_queue = DeferredQueue()
        log.info('PB-server-started-on-port', port=self.pb_server_port)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self):
        reactor.listenTCP(24497, pb.PBServerFactory(self))
        self.remote = None
        self.response_queue = DeferredQueue()
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self, adapter_agent, config):
        self.adapter_agent = adapter_agent
        self.config = config
        self.descriptor = Adapter(
            id=self.name,
            vendor='Voltha project',
            version='0.1',
            config=AdapterConfig(log_level=LogLevel.INFO)
        )
        self.incoming_messages = DeferredQueue()
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self, adapter_agent, config):
        self.adapter_agent = adapter_agent
        self.config = config
        self.descriptor = Adapter(
            id=self.name,
            vendor='Tibit Communications Inc.',
            version='0.1',
            config=AdapterConfig(log_level=LogLevel.INFO)
        )
        self.incoming_messages = DeferredQueue()
        self.mode = "GPON"
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self, adapter, device_id):
        self.adapter = adapter
        self.adapter_agent = adapter.adapter_agent
        self.device_id = device_id
        self.log = structlog.get_logger(device_id=device_id)
        self.incoming_messages = DeferredQueue()
        self.proxy_address = None
        # reference of uni_port is required when re-enabling the device if
        # it was disabled previously
        self.uni_port = None
        self.pon_port = None
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def __init__(self):
        self.queue = DeferredQueue()

        # work in progress
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def testQueue(self):
        N, M = 2, 2
        queue = defer.DeferredQueue(N, M)

        gotten = []

        for i in range(M):
            queue.get().addCallback(gotten.append)
        self.assertRaises(defer.QueueUnderflow, queue.get)

        for i in range(M):
            queue.put(i)
            self.assertEqual(gotten, list(range(i + 1)))
        for i in range(N):
            queue.put(N + i)
            self.assertEqual(gotten, list(range(M)))
        self.assertRaises(defer.QueueOverflow, queue.put, None)

        gotten = []
        for i in range(N):
            queue.get().addCallback(gotten.append)
            self.assertEqual(gotten, list(range(N, N + i + 1)))

        queue = defer.DeferredQueue()
        gotten = []
        for i in range(N):
            queue.get().addCallback(gotten.append)
        for i in range(N):
            queue.put(i)
        self.assertEqual(gotten, list(range(N)))

        queue = defer.DeferredQueue(size=0)
        self.assertRaises(defer.QueueOverflow, queue.put, None)

        queue = defer.DeferredQueue(backlog=0)
        self.assertRaises(defer.QueueUnderflow, queue.get)
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def test_cancelQueueAfterSynchronousGet(self):
        """
        When canceling a L{Deferred} from a L{DeferredQueue} that already has
        a result, the cancel should have no effect.
        """
        def _failOnErrback(_):
            self.fail("Unexpected errback call!")

        queue = defer.DeferredQueue()
        d = queue.get()
        d.addErrback(_failOnErrback)
        queue.put(None)
        d.cancel()
项目:txamqp    作者:txamqp    | 项目源码 | 文件源码
def __init__(self, delegate, vhost, spec, heartbeat=0, clock=None, insist=False):
        FrameReceiver.__init__(self, spec)
        self.delegate = delegate

        # XXX Cyclic dependency
        self.delegate.client = self

        self.vhost = vhost

        self.channelFactory = type("Channel%s" % self.spec.klass.__name__,
                                   (self.channelClass, self.spec.klass), {})
        self.channels = {}
        self.channelLock = defer.DeferredLock()

        self.outgoing = defer.DeferredQueue()
        self.work = defer.DeferredQueue()

        self.started = TwistedEvent()
        self.disconnected = TwistedEvent()  # Fired upon connection shutdown
        self.closed = False

        self.queueLock = defer.DeferredLock()
        self.basic_return_queue = TimeoutDeferredQueue()

        self.queues = {}

        self.outgoing.get().addCallback(self.writer)
        self.work.get().addCallback(self.worker)
        self.heartbeatInterval = heartbeat
        self.insist = insist
        if clock is None:
            from twisted.internet import reactor
            clock = reactor
        self.clock = clock
        if self.heartbeatInterval > 0:
            self.checkHB = self.clock.callLater(self.heartbeatInterval *
                                                self.MAX_UNSEEN_HEARTBEAT, self.check_heartbeat)
            self.sendHB = LoopingCall(self.send_heartbeat)
            self.sendHB.clock = self.clock
            d = self.started.wait()
            d.addCallback(lambda _: self.reschedule_send_heartbeat())
            d.addCallback(lambda _: self.reschedule_check_heartbeat())
            # If self.started fails, don't start the heartbeat.
            d.addErrback(lambda _: None)