2021-02-24 18:39:14 +00:00
|
|
|
import trio
|
2020-01-31 22:06:26 +00:00
|
|
|
import tractor
|
|
|
|
|
|
2026-08-29 22:58:43 +00:00
|
|
|
_this_module: str = __name__
|
|
|
|
|
the_line: str = 'Hi my name is {}'
|
2020-01-31 22:06:26 +00:00
|
|
|
|
|
|
|
|
|
2026-08-29 22:58:43 +00:00
|
|
|
tractor.log.get_console_log('INFO')
|
2020-01-31 22:06:26 +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
|
|
|
async def hi() -> str:
|
2026-08-29 22:58:43 +00:00
|
|
|
'''
|
|
|
|
|
Return a greeting naming the current actor.
|
|
|
|
|
|
|
|
|
|
'''
|
2020-01-31 22:06:26 +00:00
|
|
|
return the_line.format(tractor.current_actor().name)
|
|
|
|
|
|
|
|
|
|
|
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 say_hello(other_actor: str) -> str:
|
2026-08-29 22:58:43 +00:00
|
|
|
'''
|
|
|
|
|
Ask another actor to return its greeting.
|
|
|
|
|
|
|
|
|
|
'''
|
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
|
2020-01-31 22:06:26 +00:00
|
|
|
async with tractor.wait_for_actor(other_actor) as portal:
|
2020-12-21 14:09:55 +00:00
|
|
|
return await portal.run(hi)
|
2020-01-31 22:06:26 +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
|
|
|
async def main() -> None:
|
2026-08-29 22:58:43 +00:00
|
|
|
'''
|
|
|
|
|
Main tractor entry point, the "master" process (for now
|
2020-01-31 22:06:26 +00:00
|
|
|
acts as the "director").
|
2026-08-29 22:58:43 +00:00
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
an: tractor.ActorNursery
|
2026-08-20 03:29:02 +00:00
|
|
|
async with tractor.open_nursery() as an:
|
2026-08-29 22:58:43 +00:00
|
|
|
print('Alright... Action!')
|
2020-01-31 22:06:26 +00:00
|
|
|
|
2026-08-20 03:29:02 +00:00
|
|
|
# both actors wait on (then dial!) the *other*, so each
|
|
|
|
|
# must outlive both hellos: spawn as daemons, run the
|
|
|
|
|
# hellos concurrently, reap only once both complete.
|
|
|
|
|
portals: dict[str, tractor.Portal] = {
|
|
|
|
|
name: await an.start_actor(
|
|
|
|
|
name,
|
|
|
|
|
enable_modules=[__name__],
|
|
|
|
|
)
|
|
|
|
|
for name in ('donny', 'gretchen')
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-25 19:20:34 +00:00
|
|
|
async def run_and_print(
|
|
|
|
|
name: str,
|
|
|
|
|
other_actor: str,
|
|
|
|
|
) -> None:
|
2026-08-29 22:58:43 +00:00
|
|
|
'''
|
|
|
|
|
Print a greeting fetched through a named actor.
|
|
|
|
|
|
|
|
|
|
'''
|
2026-08-20 03:29:02 +00:00
|
|
|
print(
|
2026-08-25 19:20:34 +00:00
|
|
|
# RPC through an existing actor's `Portal`.
|
2026-08-20 03:29:02 +00:00
|
|
|
await portals[name].run(
|
|
|
|
|
say_hello,
|
|
|
|
|
other_actor=other_actor,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-29 22:58:43 +00:00
|
|
|
tn: trio.Nursery
|
2026-08-20 03:29:02 +00:00
|
|
|
async with trio.open_nursery() as tn:
|
|
|
|
|
tn.start_soon(run_and_print, 'donny', 'gretchen')
|
|
|
|
|
tn.start_soon(run_and_print, 'gretchen', 'donny')
|
|
|
|
|
|
|
|
|
|
await an.cancel()
|
|
|
|
|
|
2026-08-29 22:58:43 +00:00
|
|
|
print('CUTTTT CUUTT CUT!!! Donny!! You\'re supposed to say...')
|
2020-01-31 22:06:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-02-24 18:39:14 +00:00
|
|
|
trio.run(main)
|