From 6b8393a4d68aaa105d140ab8f1e22fa4fd04ec7b Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sat, 8 Sep 2018 09:39:53 -0400 Subject: [PATCH] Add `tractor.run_daemon()` for running a main rpc daemon --- tractor/__init__.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tractor/__init__.py b/tractor/__init__.py index d48e257..aac8036 100644 --- a/tractor/__init__.py +++ b/tractor/__init__.py @@ -2,7 +2,9 @@ tractor: An actor model micro-framework built on ``trio`` and ``multiprocessing``. """ +import importlib from functools import partial +from typing import Tuple, Any import typing import trio # type: ignore @@ -35,10 +37,10 @@ _default_arbiter_port = 1616 async def _main( async_fn: typing.Callable[..., typing.Awaitable], - args: typing.Tuple, + args: Tuple, kwargs: typing.Dict[str, typing.Any], name: str, - arbiter_addr: typing.Tuple[str, int] + arbiter_addr: Tuple[str, int] ) -> typing.Any: """Async entry point for ``tractor``. """ @@ -81,13 +83,28 @@ async def _main( def run( async_fn: typing.Callable[..., typing.Awaitable], - *args: typing.Tuple, + *args: Tuple, name: str = None, - arbiter_addr: typing.Tuple[str, int] = (_default_arbiter_host, _default_arbiter_port), + arbiter_addr: Tuple[str, int] = ( + _default_arbiter_host, _default_arbiter_port), **kwargs: typing.Dict[str, typing.Any], -): +) -> Any: """Run a trio-actor async function in process. This is tractor's main entry and the start point for any async actor. """ return trio.run(_main, async_fn, args, kwargs, name, arbiter_addr) + + +def run_daemon( + rpc_modules: Tuple[str] = (), + **kwargs +) -> None: + for path in rpc_modules: + importlib.import_module(path) + + return run( + partial(trio.sleep, float('inf')), + rpc_module_paths=rpc_modules, + **kwargs + )