Compare commits
3 Commits
c025761f15
...
4c3c3e4b56
Author | SHA1 | Date |
---|---|---|
Tyler Goodlet | 4c3c3e4b56 | |
Tyler Goodlet | b29d33d603 | |
Tyler Goodlet | 1617e0ff2c |
|
@ -196,8 +196,8 @@ async def _drain_to_final_msg(
|
||||||
(ctx._stream.closed
|
(ctx._stream.closed
|
||||||
and (reason := 'stream was already closed')
|
and (reason := 'stream was already closed')
|
||||||
)
|
)
|
||||||
or (ctx._cancelled_caught
|
or (ctx.cancel_acked
|
||||||
and (reason := 'ctx caught a cancel')
|
and (reason := 'ctx cancelled other side')
|
||||||
)
|
)
|
||||||
or (ctx._cancel_called
|
or (ctx._cancel_called
|
||||||
and (reason := 'ctx called `.cancel()`')
|
and (reason := 'ctx called `.cancel()`')
|
||||||
|
|
|
@ -348,6 +348,7 @@ async def open_root_actor(
|
||||||
await actor.cancel(None) # self cancel
|
await actor.cancel(None) # self cancel
|
||||||
finally:
|
finally:
|
||||||
_state._current_actor = None
|
_state._current_actor = None
|
||||||
|
_state._last_actor_terminated = actor
|
||||||
|
|
||||||
# restore built-in `breakpoint()` hook state
|
# restore built-in `breakpoint()` hook state
|
||||||
sys.breakpointhook = builtin_bp_handler
|
sys.breakpointhook = builtin_bp_handler
|
||||||
|
|
|
@ -1752,8 +1752,8 @@ class Actor:
|
||||||
self,
|
self,
|
||||||
cid: str,
|
cid: str,
|
||||||
parent_chan: Channel,
|
parent_chan: Channel,
|
||||||
|
requesting_uid: tuple[str, str]|None,
|
||||||
|
|
||||||
requesting_uid: tuple[str, str]|None = None,
|
|
||||||
ipc_msg: dict|None|bool = False,
|
ipc_msg: dict|None|bool = False,
|
||||||
|
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
@ -2352,6 +2352,7 @@ async def process_messages(
|
||||||
await actor._cancel_task(
|
await actor._cancel_task(
|
||||||
cid,
|
cid,
|
||||||
channel,
|
channel,
|
||||||
|
requesting_uid=channel.uid,
|
||||||
|
|
||||||
ipc_msg=msg,
|
ipc_msg=msg,
|
||||||
)
|
)
|
||||||
|
|
|
@ -18,12 +18,18 @@
|
||||||
Per process state
|
Per process state
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
from typing import (
|
from typing import (
|
||||||
Optional,
|
|
||||||
Any,
|
Any,
|
||||||
|
TYPE_CHECKING,
|
||||||
)
|
)
|
||||||
|
|
||||||
_current_actor: Optional['Actor'] = None # type: ignore # noqa
|
if TYPE_CHECKING:
|
||||||
|
from ._runtime import Actor
|
||||||
|
|
||||||
|
|
||||||
|
_current_actor: Actor|None = None # type: ignore # noqa
|
||||||
|
_last_actor_terminated: Actor|None = None
|
||||||
_runtime_vars: dict[str, Any] = {
|
_runtime_vars: dict[str, Any] = {
|
||||||
'_debug_mode': False,
|
'_debug_mode': False,
|
||||||
'_is_root': False,
|
'_is_root': False,
|
||||||
|
@ -31,14 +37,49 @@ _runtime_vars: dict[str, Any] = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def current_actor(err_on_no_runtime: bool = True) -> 'Actor': # type: ignore # noqa
|
def last_actor() -> Actor|None:
|
||||||
|
'''
|
||||||
|
Try to return last active `Actor` singleton
|
||||||
|
for this process.
|
||||||
|
|
||||||
|
For case where runtime already exited but someone is asking
|
||||||
|
about the "last" actor probably to get its `.uid: tuple`.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return _last_actor_terminated
|
||||||
|
|
||||||
|
|
||||||
|
def current_actor(
|
||||||
|
err_on_no_runtime: bool = True,
|
||||||
|
) -> Actor:
|
||||||
'''
|
'''
|
||||||
Get the process-local actor instance.
|
Get the process-local actor instance.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
if (
|
||||||
|
err_on_no_runtime
|
||||||
|
and _current_actor is None
|
||||||
|
):
|
||||||
|
msg: str = 'No local actor has been initialized yet'
|
||||||
from ._exceptions import NoRuntime
|
from ._exceptions import NoRuntime
|
||||||
if _current_actor is None and err_on_no_runtime:
|
|
||||||
raise NoRuntime("No local actor has been initialized yet")
|
if last := last_actor():
|
||||||
|
msg += (
|
||||||
|
f'Apparently the lact active actor was\n'
|
||||||
|
f'|_{last}\n'
|
||||||
|
f'|_{last.uid}\n'
|
||||||
|
)
|
||||||
|
# no actor runtime has (as of yet) ever been started for
|
||||||
|
# this process.
|
||||||
|
else:
|
||||||
|
msg += (
|
||||||
|
'No last actor found?\n'
|
||||||
|
'Did you forget to open one of:\n\n'
|
||||||
|
'- `tractor.open_root_actor()`\n'
|
||||||
|
'- `tractor.open_nursery()`\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
raise NoRuntime(msg)
|
||||||
|
|
||||||
return _current_actor
|
return _current_actor
|
||||||
|
|
||||||
|
|
|
@ -533,12 +533,15 @@ async def open_nursery(
|
||||||
|
|
||||||
'''
|
'''
|
||||||
implicit_runtime: bool = False
|
implicit_runtime: bool = False
|
||||||
|
actor: Actor = current_actor(
|
||||||
actor = current_actor(err_on_no_runtime=False)
|
err_on_no_runtime=False
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if actor is None and is_main_process():
|
if (
|
||||||
|
actor is None
|
||||||
|
and is_main_process()
|
||||||
|
):
|
||||||
# if we are the parent process start the
|
# if we are the parent process start the
|
||||||
# actor runtime implicitly
|
# actor runtime implicitly
|
||||||
log.info("Starting actor runtime!")
|
log.info("Starting actor runtime!")
|
||||||
|
|
Loading…
Reference in New Issue