From 20f9ccfa9e62b06c669be6f677111ae47ca76ca1 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 10 Feb 2020 12:08:14 -0500 Subject: [PATCH] Move two more examples out of docs for testing --- docs/index.rst | 57 +------------------ ...ctor_spawning_and_causality_with_daemon.py | 33 +++++++++++ examples/remote_error_propagation.py | 29 ++++++++++ 3 files changed, 64 insertions(+), 55 deletions(-) create mode 100644 examples/actor_spawning_and_causality_with_daemon.py create mode 100644 examples/remote_error_propagation.py diff --git a/docs/index.rst b/docs/index.rst index 45d7698..4627546 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -180,35 +180,7 @@ method: Here is a similar example using the latter method: -.. code:: python - - def movie_theatre_question(): - """A question asked in a dark theatre, in a tangent - (errr, I mean different) process. - """ - return 'have you ever seen a portal?' - - - async def main(): - """The main ``tractor`` routine. - """ - async with tractor.open_nursery() as n: - - portal = await n.start_actor( - 'frank', - # enable the actor to run funcs from this current module - rpc_module_paths=[__name__], - ) - - print(await portal.run(__name__, 'movie_theatre_question')) - # call the subactor a 2nd time - print(await portal.run(__name__, 'movie_theatre_question')) - - # the async with will block here indefinitely waiting - # for our actor "frank" to complete, but since it's an - # "outlive_main" actor it will never end until cancelled - await portal.cancel_actor() - +.. literalinclude:: ../examples/actor_spawning_and_causality_with_daemon.py The ``rpc_module_paths`` `kwarg` above is a list of module path strings that will be loaded and made accessible for execution in the @@ -247,32 +219,7 @@ Any task invoked in a remote actor should ship any error(s) back to the calling actor where it is raised and expected to be dealt with. This way remote actors are never cancelled unless explicitly asked or there's a bug in ``tractor`` itself. -.. code:: python - - async def assert_err(): - assert 0 - - - async def main(): - async with tractor.open_nursery() as n: - real_actors = [] - for i in range(3): - real_actors.append(await n.start_actor( - f'actor_{i}', - rpc_module_paths=[__name__], - )) - - # start one actor that will fail immediately - await n.run_in_actor('extra', assert_err) - - # should error here with a ``RemoteActorError`` containing - # an ``AssertionError`` and all the other actors have been cancelled - - try: - # also raises - tractor.run(main) - except tractor.RemoteActorError: - print("Look Maa that actor failed hard, hehhh!") +.. literalinclude:: ../examples/remote_error_propagation.py You'll notice the nursery cancellation conducts a *one-cancels-all* diff --git a/examples/actor_spawning_and_causality_with_daemon.py b/examples/actor_spawning_and_causality_with_daemon.py new file mode 100644 index 0000000..bb6f4ee --- /dev/null +++ b/examples/actor_spawning_and_causality_with_daemon.py @@ -0,0 +1,33 @@ +import tractor + + +def movie_theatre_question(): + """A question asked in a dark theatre, in a tangent + (errr, I mean different) process. + """ + return 'have you ever seen a portal?' + + +async def main(): + """The main ``tractor`` routine. + """ + async with tractor.open_nursery() as n: + + portal = await n.start_actor( + 'frank', + # enable the actor to run funcs from this current module + rpc_module_paths=[__name__], + ) + + print(await portal.run(__name__, 'movie_theatre_question')) + # call the subactor a 2nd time + print(await portal.run(__name__, 'movie_theatre_question')) + + # the async with will block here indefinitely waiting + # for our actor "frank" to complete, but since it's an + # "outlive_main" actor it will never end until cancelled + await portal.cancel_actor() + + +if __name__ == '__main__': + tractor.run(main) diff --git a/examples/remote_error_propagation.py b/examples/remote_error_propagation.py new file mode 100644 index 0000000..29528f5 --- /dev/null +++ b/examples/remote_error_propagation.py @@ -0,0 +1,29 @@ +import tractor + + +async def assert_err(): + assert 0 + + +async def main(): + async with tractor.open_nursery() as n: + real_actors = [] + for i in range(3): + real_actors.append(await n.start_actor( + f'actor_{i}', + rpc_module_paths=[__name__], + )) + + # start one actor that will fail immediately + await n.run_in_actor('extra', assert_err) + + # should error here with a ``RemoteActorError`` containing + # an ``AssertionError`` and all the other actors have been cancelled + + +if __name__ == '__main__': + try: + # also raises + tractor.run(main) + except tractor.RemoteActorError: + print("Look Maa that actor failed hard, hehhh!")