Port non-debugging examples off `run_in_actor`

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
drop_ria_nursery
Gud Boi 2026-07-06 12:11:39 -04:00
parent cb6202e3cb
commit d8af5f125a
4 changed files with 42 additions and 35 deletions

View File

@ -17,26 +17,37 @@ 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())
# 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...")

View File

@ -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__':

View File

@ -25,17 +25,15 @@ async def burn_cpu():
async def main():
async with tractor.open_nursery() as n:
portal = await n.run_in_actor(burn_cpu)
async with trio.open_nursery() as tn:
# burn rubber in the parent too
await burn_cpu()
tn.start_soon(burn_cpu)
# wait on result from target function
pid = await portal.wait_for_result()
# 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)
# end of nursery block
print(f"Collected subproc {pid}")

View File

@ -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__':