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-codedrop_ria_nursery
parent
cb6202e3cb
commit
d8af5f125a
|
|
@ -17,27 +17,38 @@ async def say_hello(other_actor):
|
||||||
return await portal.run(hi)
|
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():
|
async def main():
|
||||||
"""Main tractor entry point, the "master" process (for now
|
"""Main tractor entry point, the "master" process (for now
|
||||||
acts as the "director").
|
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!")
|
print("Alright... Action!")
|
||||||
|
|
||||||
donny = await n.run_in_actor(
|
# both actors wait on the *other* to register so their
|
||||||
say_hello,
|
# one-shots must run concurrently.
|
||||||
name='donny',
|
tn.start_soon(run_and_print, an, 'donny', 'gretchen')
|
||||||
# arguments are always named
|
tn.start_soon(run_and_print, an, 'gretchen', 'donny')
|
||||||
other_actor='gretchen',
|
|
||||||
)
|
print("CUTTTT CUUTT CUT!!! Donny!! You're supposed to say...")
|
||||||
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...")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
|
|
@ -10,17 +10,14 @@ async def cellar_door():
|
||||||
async def main():
|
async def main():
|
||||||
"""The main ``tractor`` routine.
|
"""The main ``tractor`` routine.
|
||||||
"""
|
"""
|
||||||
async with tractor.open_nursery() as n:
|
# spawn a subactor, run ``cellar_door()`` as its lone task,
|
||||||
|
# block until its result arrives and the subactor is reaped.
|
||||||
portal = await n.run_in_actor(
|
print(
|
||||||
|
await tractor.to_actor.run(
|
||||||
cellar_door,
|
cellar_door,
|
||||||
name='some_linguist',
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
|
|
@ -25,17 +25,15 @@ async def burn_cpu():
|
||||||
|
|
||||||
async def main():
|
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
|
# run the same func as the lone task in a subactor,
|
||||||
await burn_cpu()
|
# 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}")
|
print(f"Collected subproc {pid}")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,11 +15,12 @@ async def main():
|
||||||
enable_modules=[__name__],
|
enable_modules=[__name__],
|
||||||
))
|
))
|
||||||
|
|
||||||
# start one actor that will fail immediately
|
# run one one-shot task actor that will fail immediately;
|
||||||
await n.run_in_actor(assert_err)
|
# 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
|
# ..as a ``RemoteActorError`` containing an ``AssertionError``
|
||||||
# an ``AssertionError`` and all the other actors have been cancelled
|
# and all the other actors have been cancelled
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue