2021-12-10 18:48:41 +00:00
|
|
|
'''
|
|
|
|
|
An SC compliant infected ``asyncio`` echo server.
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
import asyncio
|
|
|
|
|
from statistics import mean
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
import trio
|
|
|
|
|
import tractor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def aio_echo_server(
|
2026-03-10 22:28:50 +00:00
|
|
|
chan: tractor.to_asyncio.LinkedTaskChannel,
|
2021-12-10 18:48:41 +00:00
|
|
|
) -> None:
|
|
|
|
|
|
|
|
|
|
# a first message must be sent **from** this ``asyncio``
|
|
|
|
|
# task or the ``trio`` side will never unblock from
|
|
|
|
|
# ``tractor.to_asyncio.open_channel_from():``
|
2026-03-10 22:28:50 +00:00
|
|
|
chan.started_nowait('start')
|
2021-12-10 18:48:41 +00:00
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
# echo the msg back
|
2026-03-10 22:28:50 +00:00
|
|
|
chan.send_nowait(await chan.get())
|
2021-12-11 18:09:36 +00:00
|
|
|
await asyncio.sleep(0)
|
2021-12-10 18:48:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@tractor.context
|
|
|
|
|
async def trio_to_aio_echo_server(
|
|
|
|
|
ctx: tractor.Context,
|
Type the docs-visible `examples/` scripts
Type the runtime objects (`ActorNursery`, `Portal`, `Context`,
`trio.Nursery`) + fn signatures across the 16 highest-visibility,
`literalinclude`-d `examples/` scripts, matching the front-page
`we_are_processes.py` style — so the rendered guides show typed
usage throughout, not just on the landing snippet.
Spans the 3 quickstart-backing scripts + `single_func`,
`remote_error_propagation`, `multiple_streams_one_portal`,
`quick_cluster`, `service_discovery`, `service_daemon_discovery`,
`asynchronous_generators`, `nested_actor_tree`,
`concurrent_actors_primes`, `streaming_broadcast_fanout`,
`rpc_bidir_streaming`, `infected_asyncio_echo_server`,
`typed_payloads`.
Annotation-only (no renames/logic changes); each runs green and the
docs build stays warning-free. Part of the examples-typing bullet
in #472.
(this patch was generated in some part by [`claude-code`][claude-code-gh])
[claude-code-gh]: https://github.com/anthropics/claude-code
2026-07-02 16:32:49 +00:00
|
|
|
) -> None:
|
2021-12-10 18:48:41 +00:00
|
|
|
# this will block until the ``asyncio`` task sends a "first"
|
|
|
|
|
# message.
|
|
|
|
|
async with tractor.to_asyncio.open_channel_from(
|
|
|
|
|
aio_echo_server,
|
2026-03-12 20:32:44 +00:00
|
|
|
) as (chan, first):
|
2021-12-10 18:48:41 +00:00
|
|
|
|
|
|
|
|
assert first == 'start'
|
|
|
|
|
await ctx.started(first)
|
|
|
|
|
|
|
|
|
|
async with ctx.open_stream() as stream:
|
|
|
|
|
|
|
|
|
|
async for msg in stream:
|
|
|
|
|
await chan.send(msg)
|
|
|
|
|
|
|
|
|
|
out = await chan.receive()
|
|
|
|
|
# echo back to parent actor-task
|
|
|
|
|
await stream.send(out)
|
|
|
|
|
|
|
|
|
|
|
Type the docs-visible `examples/` scripts
Type the runtime objects (`ActorNursery`, `Portal`, `Context`,
`trio.Nursery`) + fn signatures across the 16 highest-visibility,
`literalinclude`-d `examples/` scripts, matching the front-page
`we_are_processes.py` style — so the rendered guides show typed
usage throughout, not just on the landing snippet.
Spans the 3 quickstart-backing scripts + `single_func`,
`remote_error_propagation`, `multiple_streams_one_portal`,
`quick_cluster`, `service_discovery`, `service_daemon_discovery`,
`asynchronous_generators`, `nested_actor_tree`,
`concurrent_actors_primes`, `streaming_broadcast_fanout`,
`rpc_bidir_streaming`, `infected_asyncio_echo_server`,
`typed_payloads`.
Annotation-only (no renames/logic changes); each runs green and the
docs build stays warning-free. Part of the examples-typing bullet
in #472.
(this patch was generated in some part by [`claude-code`][claude-code-gh])
[claude-code-gh]: https://github.com/anthropics/claude-code
2026-07-02 16:32:49 +00:00
|
|
|
async def main() -> None:
|
2021-12-10 18:48:41 +00:00
|
|
|
|
Type the docs-visible `examples/` scripts
Type the runtime objects (`ActorNursery`, `Portal`, `Context`,
`trio.Nursery`) + fn signatures across the 16 highest-visibility,
`literalinclude`-d `examples/` scripts, matching the front-page
`we_are_processes.py` style — so the rendered guides show typed
usage throughout, not just on the landing snippet.
Spans the 3 quickstart-backing scripts + `single_func`,
`remote_error_propagation`, `multiple_streams_one_portal`,
`quick_cluster`, `service_discovery`, `service_daemon_discovery`,
`asynchronous_generators`, `nested_actor_tree`,
`concurrent_actors_primes`, `streaming_broadcast_fanout`,
`rpc_bidir_streaming`, `infected_asyncio_echo_server`,
`typed_payloads`.
Annotation-only (no renames/logic changes); each runs green and the
docs build stays warning-free. Part of the examples-typing bullet
in #472.
(this patch was generated in some part by [`claude-code`][claude-code-gh])
[claude-code-gh]: https://github.com/anthropics/claude-code
2026-07-02 16:32:49 +00:00
|
|
|
n: tractor.ActorNursery
|
2021-12-10 18:48:41 +00:00
|
|
|
async with tractor.open_nursery() as n:
|
Type the docs-visible `examples/` scripts
Type the runtime objects (`ActorNursery`, `Portal`, `Context`,
`trio.Nursery`) + fn signatures across the 16 highest-visibility,
`literalinclude`-d `examples/` scripts, matching the front-page
`we_are_processes.py` style — so the rendered guides show typed
usage throughout, not just on the landing snippet.
Spans the 3 quickstart-backing scripts + `single_func`,
`remote_error_propagation`, `multiple_streams_one_portal`,
`quick_cluster`, `service_discovery`, `service_daemon_discovery`,
`asynchronous_generators`, `nested_actor_tree`,
`concurrent_actors_primes`, `streaming_broadcast_fanout`,
`rpc_bidir_streaming`, `infected_asyncio_echo_server`,
`typed_payloads`.
Annotation-only (no renames/logic changes); each runs green and the
docs build stays warning-free. Part of the examples-typing bullet
in #472.
(this patch was generated in some part by [`claude-code`][claude-code-gh])
[claude-code-gh]: https://github.com/anthropics/claude-code
2026-07-02 16:32:49 +00:00
|
|
|
p: tractor.Portal = await n.start_actor(
|
2021-12-10 18:48:41 +00:00
|
|
|
'aio_server',
|
|
|
|
|
enable_modules=[__name__],
|
|
|
|
|
infect_asyncio=True,
|
|
|
|
|
)
|
|
|
|
|
async with p.open_context(
|
|
|
|
|
trio_to_aio_echo_server,
|
|
|
|
|
) as (ctx, first):
|
|
|
|
|
|
|
|
|
|
assert first == 'start'
|
|
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
async with ctx.open_stream() as stream:
|
|
|
|
|
|
|
|
|
|
delays = []
|
|
|
|
|
send = time.time()
|
|
|
|
|
|
|
|
|
|
await stream.send(count)
|
|
|
|
|
async for msg in stream:
|
|
|
|
|
recv = time.time()
|
|
|
|
|
delays.append(recv - send)
|
|
|
|
|
assert msg == count
|
|
|
|
|
count += 1
|
|
|
|
|
send = time.time()
|
|
|
|
|
await stream.send(count)
|
|
|
|
|
|
|
|
|
|
if count >= 1e3:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
print(f'mean round trip rate (Hz): {1/mean(delays)}')
|
|
|
|
|
await p.cancel_actor()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
trio.run(main)
|