Add `tractor.run_daemon()` for running a main rpc daemon

multi_program_tests
Tyler Goodlet 2018-09-08 09:39:53 -04:00
parent 0ca668453c
commit 6b8393a4d6
1 changed files with 22 additions and 5 deletions

View File

@ -2,7 +2,9 @@
tractor: An actor model micro-framework built on tractor: An actor model micro-framework built on
``trio`` and ``multiprocessing``. ``trio`` and ``multiprocessing``.
""" """
import importlib
from functools import partial from functools import partial
from typing import Tuple, Any
import typing import typing
import trio # type: ignore import trio # type: ignore
@ -35,10 +37,10 @@ _default_arbiter_port = 1616
async def _main( async def _main(
async_fn: typing.Callable[..., typing.Awaitable], async_fn: typing.Callable[..., typing.Awaitable],
args: typing.Tuple, args: Tuple,
kwargs: typing.Dict[str, typing.Any], kwargs: typing.Dict[str, typing.Any],
name: str, name: str,
arbiter_addr: typing.Tuple[str, int] arbiter_addr: Tuple[str, int]
) -> typing.Any: ) -> typing.Any:
"""Async entry point for ``tractor``. """Async entry point for ``tractor``.
""" """
@ -81,13 +83,28 @@ async def _main(
def run( def run(
async_fn: typing.Callable[..., typing.Awaitable], async_fn: typing.Callable[..., typing.Awaitable],
*args: typing.Tuple, *args: Tuple,
name: str = None, 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], **kwargs: typing.Dict[str, typing.Any],
): ) -> Any:
"""Run a trio-actor async function in process. """Run a trio-actor async function in process.
This is tractor's main entry and the start point for any async actor. 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) 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
)