diff --git a/tractor/_state.py b/tractor/_state.py index 4b214608..78937dd7 100644 --- a/tractor/_state.py +++ b/tractor/_state.py @@ -25,6 +25,7 @@ from contextvars import ( from pathlib import Path from typing import ( Any, + Callable, Literal, TYPE_CHECKING, ) @@ -32,6 +33,12 @@ from typing import ( import platformdirs from trio.lowlevel import current_task +# from .msg.pretty_struct import Struct +from msgspec import ( + field, + Struct, +) + if TYPE_CHECKING: from ._runtime import Actor from ._context import Context @@ -48,8 +55,68 @@ _current_actor: Actor|None = None # type: ignore # noqa _last_actor_terminated: Actor|None = None # TODO: mk this a `msgspec.Struct`! -# -[ ] type out all fields obvi! +# -[x] type out all fields obvi! # -[ ] (eventually) mk wire-ready for monitoring? +class RuntimeVars(Struct): + ''' + Actor-(and thus process)-global runtime state. + + This struct is relayed from parent to child during sub-actor + spawning and is a singleton instance per process. + + Generally contains, + - root-actor indicator. + - comms-info: addrs for both (public) process/service-discovery + and in-tree contact with other actors. + - transport-layer IPC protocol server(s) settings. + - debug-mode settings for enabling sync breakpointing and any + surrounding REPL-fixture hooking. + - infected-`asyncio` via guest-mode toggle(s)/cohfig. + + ''' + _is_root: bool = False # bool + _root_mailbox: tuple[str, str|int] = (None, None) # tuple[str|None, str|None] + _root_addrs: list[ + tuple[str, str|int], + ] = [] # tuple[str|None, str|None] + + # parent->chld ipc protocol caps + _enable_tpts: list[TransportProtocolKey] = field( + default_factory=lambda: [_def_tpt_proto], + ) + + # registrar info + _registry_addrs: list[tuple] = [] + + # `debug_mode: bool` settings + _debug_mode: bool = False # bool + repl_fixture: bool|Callable = False # |AbstractContextManager[bool] + # for `tractor.pause_from_sync()` & `breakpoint()` support + use_greenback: bool = False + + # infected-`asyncio`-mode: `trio` running as guest. + _is_infected_aio: bool = False + + def __setattr__( + self, + key, + val, + ) -> None: + breakpoint() + super().__setattr__(key, val) + + def update( + self, + from_dict: dict|Struct, + ) -> None: + for attr, val in from_dict.items(): + setattr( + self, + attr, + val, + ) + + _runtime_vars: dict[str, Any] = { # root of actor-process tree info '_is_root': False, # bool