本文整理汇总了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)
示例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()
示例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()
示例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
示例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)
示例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]
示例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
示例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
示例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
示例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
示例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
示例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
示例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"},
]
示例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,
)
示例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)