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


Python trio.open_memory_channel方法代碼示例

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


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

示例1: main

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def main():
    t0 = datetime.datetime.now()
    print(colorama.Fore.WHITE + "App started.", flush=True)

    """
    trio.Queue was removed in v0.11.0:
    - Replacing the call to trio.Queue() by trio.open_memory_channel()
    - Using a MemorySendChannel object in generate_data function
    - Using a MemoryReceiveChannel object in process_data function
    - Updating requirements.txt with trio v0.16.0 and trio_asyncio v0.11.0
    """

    send_channel, receive_channel = trio.open_memory_channel(max_buffer_size=10)

    with trio.move_on_after(5):
        async with trio.open_nursery() as nursery:
            nursery.start_soon(generate_data, 20, send_channel, name='Prod 1')
            nursery.start_soon(generate_data, 20, send_channel, name='Prod 2')
            nursery.start_soon(process_data, 40, receive_channel, name='Consumer')

    dt = datetime.datetime.now() - t0
    print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(
        dt.total_seconds()), flush=True) 
開發者ID:talkpython,項目名稱:async-techniques-python-course,代碼行數:25,代碼來源:prod_trio.py

示例2: test_frontier_exhaustion

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def test_frontier_exhaustion(nursery):
    # Set up test fixtures
    job_id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
    db = Mock()
    db.any_in_flight = AsyncMock()
    db.get_frontier_batch = AsyncMock(return_value=list())
    db.get_frontier_size = AsyncMock(return_value=5)
    send_channel, recv_channel = trio.open_memory_channel(0)
    login_manager = Mock()
    login_manager.login = AsyncMock()
    policy = make_policy()
    stats = dict()
    frontier = CrawlFrontier(job_id, db, send_channel, login_manager, policy,
        stats)

    # This test has an empty frontier, so it should raise an exhaustion error
    # in its run() method.
    with pytest.raises(FrontierExhaustionError):
        await frontier.run() 
開發者ID:HyperionGray,項目名稱:starbelly,代碼行數:21,代碼來源:test_frontier.py

示例3: __init__

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def __init__(self, capacity):
        '''
        Constructor.

        :param int capacity: The maximum number of items to buffer inside of the
            rate limiter.
        '''
        self._expires = list()
        self._expiry_cancel_scope = None
        self._global_limit = None
        self._queues = dict()
        self._rate_limits = dict()
        self._capacity = capacity
        self._semaphore = trio.Semaphore(capacity)
        self._request_send, self._request_recv = trio.open_memory_channel(0)
        self._reset_send, self._reset_recv = trio.open_memory_channel(0)
        self._job_channels = dict() 
開發者ID:HyperionGray,項目名稱:starbelly,代碼行數:19,代碼來源:rate_limiter.py

示例4: get_channel

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def get_channel(self, channel_size):
        '''
        Get a statistics channel. The resource monitor will send measurements to
        this channel until the receive end is closed. Note that if the channel
        is full, the resource monitor does not block! It will drop messages
        instead.

        :param int channel_size: The size of the channel to create.
        :returns: A channel that will receive resource statistics at regular
            intervals.
        :rtype: trio.ReceiveChannel
        '''
        logger.debug('Creating new channel with size=%d', channel_size)
        send_channel, recv_channel = trio.open_memory_channel(channel_size)
        self._channels.append(send_channel)
        return recv_channel 
開發者ID:HyperionGray,項目名稱:starbelly,代碼行數:18,代碼來源:resource_monitor.py

示例5: init

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def init(self, threads=1):
        '''Start worker threads'''

        self.trio_token = trio.lowlevel.current_trio_token()
        self.to_upload = trio.open_memory_channel(0)
        for _ in range(threads):
            t = threading.Thread(target=self._upload_loop)
            t.start()
            self.upload_threads.append(t)

        self.to_remove = Queue(1000)
        with self.backend_pool() as backend:
            has_delete_multi = backend.has_delete_multi

        if has_delete_multi:
            t = threading.Thread(target=self._removal_loop_multi)
            t.daemon = True # interruption will do no permanent harm
            t.start()
            self.removal_threads.append(t)
        else:
            for _ in range(20):
                t = threading.Thread(target=self._removal_loop_simple)
                t.daemon = True # interruption will do no permanent harm
                t.start()
                self.removal_threads.append(t) 
開發者ID:s3ql,項目名稱:s3ql,代碼行數:27,代碼來源:block_cache.py

示例6: test_datagram_receiver

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def test_datagram_receiver(socket_pair):
    sending_socket, receiving_socket = socket_pair
    receiver_address = receiving_socket.getsockname()
    sender_address = sending_socket.getsockname()

    send_channel, receive_channel = trio.open_memory_channel(1)
    async with background_trio_service(DatagramReceiver(receiving_socket, send_channel)):
        data = b"some packet"

        await sending_socket.sendto(data, receiver_address)
        with trio.fail_after(0.5):
            received_datagram = await receive_channel.receive()

        assert received_datagram.datagram == data
        assert received_datagram.sender_endpoint.ip_address == inet_aton(sender_address[0])
        assert received_datagram.sender_endpoint.port == sender_address[1] 
開發者ID:ethereum,項目名稱:trinity,代碼行數:18,代碼來源:test_channel_services.py

示例7: test_datagram_sender

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def test_datagram_sender(socket_pair):
    sending_socket, receiving_socket = socket_pair
    receiver_endpoint = receiving_socket.getsockname()
    sender_endpoint = sending_socket.getsockname()

    send_channel, receive_channel = trio.open_memory_channel(1)
    async with background_trio_service(DatagramSender(receive_channel, sending_socket)):
        outgoing_datagram = OutgoingDatagram(
            b"some packet",
            Endpoint(inet_aton(receiver_endpoint[0]), receiver_endpoint[1]),
        )
        await send_channel.send(outgoing_datagram)

        with trio.fail_after(0.5):
            data, sender = await receiving_socket.recvfrom(1024)
        assert data == outgoing_datagram.datagram
        assert sender == sender_endpoint 
開發者ID:ethereum,項目名稱:trinity,代碼行數:19,代碼來源:test_channel_services.py

示例8: test_packet_decoder

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def test_packet_decoder():
    datagram_send_channel, datagram_receive_channel = trio.open_memory_channel(1)
    packet_send_channel, packet_receive_channel = trio.open_memory_channel(1)

    service = PacketDecoder(datagram_receive_channel, packet_send_channel)
    async with background_trio_service(service):
        packet = AuthTagPacketFactory()
        sender_endpoint = EndpointFactory()
        await datagram_send_channel.send(IncomingDatagram(
            datagram=packet.to_wire_bytes(),
            sender_endpoint=sender_endpoint,
        ))

        with trio.fail_after(0.5):
            incoming_packet = await packet_receive_channel.receive()

        assert incoming_packet.packet == packet
        assert incoming_packet.sender_endpoint.ip_address == sender_endpoint.ip_address
        assert incoming_packet.sender_endpoint.port == sender_endpoint.port 
開發者ID:ethereum,項目名稱:trinity,代碼行數:21,代碼來源:test_channel_services.py

示例9: test_packet_encoder

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def test_packet_encoder():
    packet_send_channel, packet_receive_channel = trio.open_memory_channel(1)
    datagram_send_channel, datagram_receive_channel = trio.open_memory_channel(1)

    service = PacketEncoder(packet_receive_channel, datagram_send_channel)
    async with background_trio_service(service):
        receiver_endpoint = EndpointFactory()
        outgoing_packet = OutgoingPacket(
            packet=AuthTagPacketFactory(),
            receiver_endpoint=receiver_endpoint,
        )
        await packet_send_channel.send(outgoing_packet)

        with trio.fail_after(0.5):
            outgoing_datagram = await datagram_receive_channel.receive()

        assert outgoing_datagram.datagram == outgoing_packet.packet.to_wire_bytes()
        assert outgoing_datagram.receiver_endpoint.ip_address == receiver_endpoint.ip_address
        assert outgoing_datagram.receiver_endpoint.port == receiver_endpoint.port 
開發者ID:ethereum,項目名稱:trinity,代碼行數:21,代碼來源:test_channel_services.py

示例10: test_request_handling

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def test_request_handling(message_dispatcher,
                                incoming_message_channels,
                                remote_enr,
                                remote_endpoint):
    ping_send_channel, ping_receive_channel = trio.open_memory_channel(0)

    async with message_dispatcher.add_request_handler(PingMessage) as request_subscription:

        incoming_message = IncomingMessage(
            message=PingMessageFactory(),
            sender_endpoint=remote_endpoint,
            sender_node_id=remote_enr.node_id,
        )
        await incoming_message_channels[0].send(incoming_message)

        with trio.fail_after(1):
            handled_incoming_message = await request_subscription.receive()
        assert handled_incoming_message == incoming_message 
開發者ID:ethereum,項目名稱:trinity,代碼行數:20,代碼來源:test_message_dispatcher.py

示例11: spawn_app

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def spawn_app(
        self,
        app: ASGIFramework,
        config: Config,
        scope: dict,
        send: Callable[[dict], Awaitable[None]],
    ) -> Callable[[dict], Awaitable[None]]:
        app_send_channel, app_receive_channel = trio.open_memory_channel(config.max_app_queue_size)
        self.nursery.start_soon(_handle, app, config, scope, app_receive_channel.receive, send)
        return app_send_channel.send 
開發者ID:pgjones,項目名稱:hypercorn,代碼行數:12,代碼來源:context.py

示例12: __init__

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def __init__(self, app: ASGIFramework, config: Config) -> None:
        self.app = app
        self.config = config
        self.startup = trio.Event()
        self.shutdown = trio.Event()
        self.app_send_channel, self.app_receive_channel = trio.open_memory_channel(
            config.max_app_queue_size
        )
        self.supported = True 
開發者ID:pgjones,項目名稱:hypercorn,代碼行數:11,代碼來源:lifespan.py

示例13: test_wsgi_trio

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def test_wsgi_trio() -> None:
    middleware = TrioWSGIMiddleware(echo_body)
    scope = {
        "http_version": "1.1",
        "method": "GET",
        "path": "/",
        "query_string": b"a=b",
        "raw_path": b"/",
        "scheme": "http",
        "type": "http",
    }
    send_channel, receive_channel = trio.open_memory_channel(1)
    await send_channel.send({"type": "http.request"})

    messages = []

    async def _send(message: dict) -> None:
        nonlocal messages
        messages.append(message)

    await middleware(scope, receive_channel.receive, _send)
    assert messages == [
        {
            "headers": [(b"content-type", b"text/plain; charset=utf-8"), (b"content-length", b"0")],
            "status": 200,
            "type": "http.response.start",
        },
        {"body": bytearray(b""), "type": "http.response.body"},
    ] 
開發者ID:pgjones,項目名稱:hypercorn,代碼行數:31,代碼來源:test_wsgi.py

示例14: __init__

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def __init__(
        self,
        transport: Transport,
        handshake: ServerHandshake,
        organization_id: OrganizationID,
        device_id: DeviceID,
        human_handle: Optional[HumanHandle],
        profile: UserProfile,
        public_key: PublicKey,
        verify_key: VerifyKey,
    ):
        super().__init__(transport, handshake)
        self.organization_id = organization_id
        self.profile = profile
        self.device_id = device_id
        self.human_handle = human_handle
        self.public_key = public_key
        self.verify_key = verify_key

        self.event_bus_ctx = None  # Overwritten in BackendApp.handle_client
        self.channels = trio.open_memory_channel(100)
        self.realms = set()

        self.conn_id = self.transport.conn_id
        self.logger = self.transport.logger = self.transport.logger.bind(
            conn_id=self.conn_id,
            handshake_type=self.handshake_type.value,
            organization_id=self.organization_id,
            device_id=self.device_id,
        ) 
開發者ID:Scille,項目名稱:parsec-cloud,代碼行數:32,代碼來源:client_context.py

示例15: thread_pool_runner

# 需要導入模塊: import trio [as 別名]
# 或者: from trio import open_memory_channel [as 別名]
def thread_pool_runner(max_workers=None):
    """A trio-managed thread pool.

    This should be removed if trio decides to add support for thread pools:
    https://github.com/python-trio/trio/blob/c5497c5ac4/trio/_threads.py#L32-L128
    """
    executor = ThreadPoolExecutor(max_workers=max_workers)
    trio_token = trio.hazmat.current_trio_token()

    async def run_in_thread(fn, *args):
        send_channel, receive_channel = trio.open_memory_channel(1)

        def target():
            result = outcome.capture(fn, *args)
            trio.from_thread.run_sync(send_channel.send_nowait, result, trio_token=trio_token)

        executor.submit(target)
        result = await receive_channel.receive()
        return result.unwrap()

    # The thread pool executor cannot be used as a sync context here, as it would
    # block the trio loop. Instead, we shut the executor down in a worker thread.
    try:
        yield run_in_thread
    finally:
        with trio.CancelScope(shield=True):
            await trio.to_thread.run_sync(executor.shutdown) 
開發者ID:Scille,項目名稱:parsec-cloud,代碼行數:29,代碼來源:local_database.py


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