From d8af5f125a033b9d392ec13bc05c3619e7d805c5 Mon Sep 17 00:00:00 2001 From: goodboy Date: Mon, 6 Jul 2026 12:11:39 -0400 Subject: [PATCH] Port non-debugging examples off `run_in_actor` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 4 example scripts of the #477 removal sweep, each exercised by `test_docs_examples.py`, - `actor_spawning_and_causality.py`: the simplest possible `to_actor.run()` demo — private call-scoped nursery, block on and print the one-shot's result. - `remote_error_propagation.py`: blocking `to_actor.run(an=n)` raises the boxed `AssertionError` in the caller's task, cancelling the sibling daemons. - `parallelism/single_func.py`: bg-burn a core in the parent via a local task-nursery while the one-shot burns (and returns from) a subactor. - `a_trynamic_first_scene.py`: donny + gretchen wait on each *other* so their one-shots run concurrently in a local task-nursery against a shared `an` (mirrors the migrated `test_trynamic_trio`). Gate: all 4 green via the example-runner suite. (this patch was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-code --- examples/a_trynamic_first_scene.py | 41 +++++++++++++++--------- examples/actor_spawning_and_causality.py | 13 +++----- examples/parallelism/single_func.py | 14 ++++---- examples/remote_error_propagation.py | 9 +++--- 4 files changed, 42 insertions(+), 35 deletions(-) diff --git a/examples/a_trynamic_first_scene.py b/examples/a_trynamic_first_scene.py index 05d61ba9..808b8079 100644 --- a/examples/a_trynamic_first_scene.py +++ b/examples/a_trynamic_first_scene.py @@ -17,27 +17,38 @@ async def say_hello(other_actor): return await portal.run(hi) +async def run_and_print( + an: tractor.ActorNursery, + name: str, + other_actor: str, +): + print( + await tractor.to_actor.run( + say_hello, + an=an, + name=name, + # arguments are always named + other_actor=other_actor, + ) + ) + + async def main(): """Main tractor entry point, the "master" process (for now acts as the "director"). """ - async with tractor.open_nursery() as n: + async with ( + tractor.open_nursery() as an, + trio.open_nursery() as tn, + ): print("Alright... Action!") - donny = await n.run_in_actor( - say_hello, - name='donny', - # arguments are always named - other_actor='gretchen', - ) - gretchen = await n.run_in_actor( - say_hello, - name='gretchen', - other_actor='donny', - ) - print(await gretchen.wait_for_result()) - print(await donny.wait_for_result()) - print("CUTTTT CUUTT CUT!!! Donny!! You're supposed to say...") + # both actors wait on the *other* to register so their + # one-shots must run concurrently. + tn.start_soon(run_and_print, an, 'donny', 'gretchen') + tn.start_soon(run_and_print, an, 'gretchen', 'donny') + + print("CUTTTT CUUTT CUT!!! Donny!! You're supposed to say...") if __name__ == '__main__': diff --git a/examples/actor_spawning_and_causality.py b/examples/actor_spawning_and_causality.py index 2232ae3e..00dc645c 100644 --- a/examples/actor_spawning_and_causality.py +++ b/examples/actor_spawning_and_causality.py @@ -10,17 +10,14 @@ async def cellar_door(): async def main(): """The main ``tractor`` routine. """ - async with tractor.open_nursery() as n: - - portal = await n.run_in_actor( + # spawn a subactor, run ``cellar_door()`` as its lone task, + # block until its result arrives and the subactor is reaped. + print( + await tractor.to_actor.run( cellar_door, name='some_linguist', ) - - # The ``async with`` will unblock here since the 'some_linguist' - # actor has completed its main task ``cellar_door``. - - print(await portal.wait_for_result()) + ) if __name__ == '__main__': diff --git a/examples/parallelism/single_func.py b/examples/parallelism/single_func.py index c4ea29e3..f99bb8c7 100644 --- a/examples/parallelism/single_func.py +++ b/examples/parallelism/single_func.py @@ -25,17 +25,15 @@ async def burn_cpu(): async def main(): - async with tractor.open_nursery() as n: + async with trio.open_nursery() as tn: - portal = await n.run_in_actor(burn_cpu) + # burn rubber in the parent too + tn.start_soon(burn_cpu) - # burn rubber in the parent too - await burn_cpu() + # run the same func as the lone task in a subactor, + # block on (and collect) its result + pid = await tractor.to_actor.run(burn_cpu) - # wait on result from target function - pid = await portal.wait_for_result() - - # end of nursery block print(f"Collected subproc {pid}") diff --git a/examples/remote_error_propagation.py b/examples/remote_error_propagation.py index aa2a73b4..0558bdc7 100644 --- a/examples/remote_error_propagation.py +++ b/examples/remote_error_propagation.py @@ -15,11 +15,12 @@ async def main(): enable_modules=[__name__], )) - # start one actor that will fail immediately - await n.run_in_actor(assert_err) + # run one one-shot task actor that will fail immediately; + # its error raises right here in the caller's task.. + await tractor.to_actor.run(assert_err, an=n) - # should error here with a ``RemoteActorError`` containing - # an ``AssertionError`` and all the other actors have been cancelled + # ..as a ``RemoteActorError`` containing an ``AssertionError`` + # and all the other actors have been cancelled if __name__ == '__main__':