Python trio.open_memory_channel() Examples
The following are 30
code examples of trio.open_memory_channel().
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
trio
, or try the search function
.
Example #1
Source File: resource_monitor.py From starbelly with MIT License | 6 votes |
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
Example #2
Source File: rate_limiter.py From starbelly with MIT License | 6 votes |
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()
Example #3
Source File: prod_trio.py From async-techniques-python-course with MIT License | 6 votes |
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)
Example #4
Source File: block_cache.py From s3ql with GNU General Public License v3.0 | 6 votes |
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)
Example #5
Source File: test_message_dispatcher.py From trinity with MIT License | 6 votes |
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
Example #6
Source File: test_channel_services.py From trinity with MIT License | 6 votes |
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]
Example #7
Source File: test_channel_services.py From trinity with MIT License | 6 votes |
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
Example #8
Source File: test_channel_services.py From trinity with MIT License | 6 votes |
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
Example #9
Source File: test_frontier.py From starbelly with MIT License | 6 votes |
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()
Example #10
Source File: test_channel_services.py From trinity with MIT License | 6 votes |
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
Example #11
Source File: test_packer.py From trinity with MIT License | 5 votes |
def remote_incoming_packet_channels(): return trio.open_memory_channel(1)
Example #12
Source File: endpoint.py From lahja with MIT License | 5 votes |
def __init__(self, socket: trio.SocketStream) -> None: self._socket = socket self._msg_send_channel, self._msg_receive_channel = cast( Tuple[trio.abc.SendChannel[Message], trio.abc.ReceiveChannel[Message]], trio.open_memory_channel(100), ) self._write_lock = trio.Lock() super().__init__()
Example #13
Source File: test_channel_services.py From trinity with MIT License | 5 votes |
def test_packet_decoder_error(): 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): # send invalid packet await datagram_send_channel.send(IncomingDatagram( datagram=b"not a valid packet", sender_endpoint=EndpointFactory(), )) # send valid packet packet = AuthTagPacketFactory() sender_endpoint = EndpointFactory() await datagram_send_channel.send(IncomingDatagram( datagram=packet.to_wire_bytes(), sender_endpoint=sender_endpoint, )) # ignore the invalid one, only receive the valid one 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
Example #14
Source File: test_packer.py From trinity with MIT License | 5 votes |
def incoming_message_channels(): return trio.open_memory_channel(1)
Example #15
Source File: test_packer.py From trinity with MIT License | 5 votes |
def outgoing_packet_channels(): return trio.open_memory_channel(1)
Example #16
Source File: test_packer.py From trinity with MIT License | 5 votes |
def outgoing_message_channels(): return trio.open_memory_channel(1)
Example #17
Source File: lifespan.py From hypercorn with MIT License | 5 votes |
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
Example #18
Source File: test_packer.py From trinity with MIT License | 5 votes |
def remote_incoming_message_channels(): return trio.open_memory_channel(1)
Example #19
Source File: test_packer.py From trinity with MIT License | 5 votes |
def remote_outgoing_message_channels(): return trio.open_memory_channel(1)
Example #20
Source File: test_endpoint_tracker.py From trinity with MIT License | 5 votes |
def vote_channels(): return trio.open_memory_channel(0)
Example #21
Source File: test_routing_table_manager.py From trinity with MIT License | 5 votes |
def incoming_message_channels(): return trio.open_memory_channel(0)
Example #22
Source File: test_routing_table_manager.py From trinity with MIT License | 5 votes |
def outgoing_message_channels(): return trio.open_memory_channel(0)
Example #23
Source File: test_routing_table_manager.py From trinity with MIT License | 5 votes |
def endpoint_vote_channels(): return trio.open_memory_channel(0)
Example #24
Source File: test_message_dispatcher.py From trinity with MIT License | 5 votes |
def outgoing_message_channels(): return trio.open_memory_channel(0)
Example #25
Source File: discovery.py From trinity with MIT License | 5 votes |
def request_enr(self, remote: NodeAPI) -> ENR: """Get the most recent ENR for the given node and update our local DB and routing table. The updating of the DB and RT happens in the handler called when we receive an ENR response, so that the new ENR is stored even if we give up waiting. Raises CouldNotRetrieveENR if we can't get a ENR from the remote node. """ # No need to use a timeout because bond() takes care of that internally. await self.bond(remote.id) token = self.send_enr_request(remote) send_chan, recv_chan = trio.open_memory_channel[Tuple[ENR, Hash32]](1) try: with trio.fail_after(constants.KADEMLIA_REQUEST_TIMEOUT): enr, received_token = await self.enr_response_channels.receive_one( remote, send_chan, recv_chan) except trio.TooSlowError: raise CouldNotRetrieveENR(f"Timed out waiting for ENR from {remote}") except AlreadyWaitingDiscoveryResponse: raise CouldNotRetrieveENR(f"Already waiting for ENR from {remote}") if received_token != token: raise CouldNotRetrieveENR( f"Got ENR from {remote} with token {received_token!r} but expected {token!r}") return enr
Example #26
Source File: packer.py From trinity with MIT License | 5 votes |
def register_peer_packer(self, remote_node_id: NodeID) -> None: if self.is_peer_packer_registered(remote_node_id): raise ValueError(f"Peer packer for {encode_hex(remote_node_id)} is already registered") incoming_packet_channels: Tuple[ SendChannel[IncomingPacket], ReceiveChannel[IncomingPacket], ] = trio.open_memory_channel(0) outgoing_message_channels: Tuple[ SendChannel[OutgoingMessage], ReceiveChannel[OutgoingMessage], ] = trio.open_memory_channel(0) peer_packer = PeerPacker( local_private_key=self.local_private_key, local_node_id=self.local_node_id, remote_node_id=remote_node_id, node_db=self.node_db, message_type_registry=self.message_type_registry, incoming_packet_receive_channel=incoming_packet_channels[1], # These channels are the standard `trio.abc.XXXChannel` interfaces. # The `clone` method is only available on `MemoryXXXChannel` types # which trio currently doesn't expose in a way that allows us to # type these channels as those types. Thus, we need to tell mypy # to ignore this since it doesn't recognize the standard # `trio.abc.XXXChannel` interfaces as having a `clone()` method. incoming_message_send_channel=self.incoming_message_send_channel.clone(), # type: ignore # noqa: E501 outgoing_message_receive_channel=outgoing_message_channels[1], outgoing_packet_send_channel=self.outgoing_packet_send_channel.clone(), # type: ignore ) manager = TrioManager(peer_packer) self.managed_peer_packers[remote_node_id] = ManagedPeerPacker( peer_packer=peer_packer, manager=manager, incoming_packet_send_channel=incoming_packet_channels[0], outgoing_message_send_channel=outgoing_message_channels[0], )
Example #27
Source File: message_dispatcher.py From trinity with MIT License | 5 votes |
def add_request_handler(self, message_class: Type[BaseMessage], ) -> IncomingMessageSubscription: message_type = message_class.message_type if message_type in self.request_handler_send_channels: raise ValueError(f"Request handler for {message_class.__name__} is already added") request_channels: Tuple[ SendChannel[IncomingMessage], ReceiveChannel[IncomingMessage], ] = trio.open_memory_channel(0) self.request_handler_send_channels[message_type] = request_channels[0] self.logger.debug("Adding request handler for %s", message_class.__name__) def remove() -> None: try: self.request_handler_send_channels.pop(message_type) except KeyError: raise ValueError( f"Request handler for {message_class.__name__} has already been removed" ) else: self.logger.debug( "Removing request handler for %s", message_class.__name__, ) return ChannelHandlerSubscription( send_channel=request_channels[0], receive_channel=request_channels[1], remove_fn=remove, )
Example #28
Source File: message_dispatcher.py From trinity with MIT License | 5 votes |
def request_response_subscription(self, receiver_node_id: NodeID, message: BaseMessage, endpoint: Optional[Endpoint] = None, ) -> AsyncGenerator[IncomingMessageSubscription, None]: if endpoint is None: endpoint = await self.get_endpoint_from_node_db(receiver_node_id) response_channels: Tuple[ SendChannel[IncomingMessage], ReceiveChannel[IncomingMessage], ] = trio.open_memory_channel(0) response_send_channel, response_receive_channel = response_channels async with self.add_response_handler( receiver_node_id, message.request_id, ) as response_subscription: outgoing_message = OutgoingMessage( message=message, receiver_node_id=receiver_node_id, receiver_endpoint=endpoint, ) self.logger.debug( "Sending %s to %s with request id %d", outgoing_message, encode_hex(receiver_node_id), message.request_id, ) await self.outgoing_message_send_channel.send(outgoing_message) yield response_subscription
Example #29
Source File: test_subscription.py From starbelly with MIT License | 5 votes |
def __init__(self): self._send, self._recv = trio.open_memory_channel(0)
Example #30
Source File: client_context.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
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, )