2021-07-31 16:10:25 +00:00
|
|
|
import trio
|
|
|
|
|
import tractor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@tractor.context
|
|
|
|
|
async def simple_rpc(
|
|
|
|
|
|
|
|
|
|
ctx: tractor.Context,
|
|
|
|
|
data: int,
|
|
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
'''Test a small ping-pong 2-way streaming server.
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
# signal to parent that we're up much like
|
2024-03-13 22:41:24 +00:00
|
|
|
# ``trio.TaskStatus.started()``
|
2021-07-31 16:10:25 +00:00
|
|
|
await ctx.started(data + 1)
|
|
|
|
|
|
|
|
|
|
async with ctx.open_stream() as stream:
|
|
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
async for msg in stream:
|
|
|
|
|
|
|
|
|
|
assert msg == 'ping'
|
|
|
|
|
await stream.send('pong')
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
assert count == 10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main() -> None:
|
|
|
|
|
|
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-07-31 16:10:25 +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
|
|
|
portal: tractor.Portal = await n.start_actor(
|
2021-07-31 16:10:25 +00:00
|
|
|
'rpc_server',
|
|
|
|
|
enable_modules=[__name__],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# XXX: syntax requires py3.9
|
|
|
|
|
async with (
|
|
|
|
|
|
|
|
|
|
portal.open_context(
|
|
|
|
|
simple_rpc, # taken from pytest parameterization
|
|
|
|
|
data=10,
|
|
|
|
|
|
|
|
|
|
) as (ctx, sent),
|
|
|
|
|
|
|
|
|
|
ctx.open_stream() as stream,
|
|
|
|
|
):
|
|
|
|
|
|
|
|
|
|
assert sent == 11
|
|
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
# receive msgs using async for style
|
|
|
|
|
await stream.send('ping')
|
|
|
|
|
|
|
|
|
|
async for msg in stream:
|
|
|
|
|
assert msg == 'pong'
|
|
|
|
|
await stream.send('ping')
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
|
|
if count >= 9:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# explicitly teardown the daemon-actor
|
|
|
|
|
await portal.cancel_actor()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
trio.run(main)
|