Compare commits
83 Commits
9e24a01d55
...
3749720d74
Author | SHA1 | Date |
---|---|---|
|
3749720d74 | |
|
7d15b3ea4b | |
|
262008f784 | |
|
059903bf81 | |
|
900e0b4cd1 | |
|
38a4e37d47 | |
|
a06289c47d | |
|
eeb748a206 | |
|
5fc64107e5 | |
|
6348c83d28 | |
|
8b8390e83c | |
|
f5c6fc2f02 | |
|
444b9bfc22 | |
|
79e70a9b08 | |
|
23240c31e3 | |
|
6a82bab627 | |
|
b485297411 | |
|
dd23ef1d95 | |
|
2ec3ff46cd | |
|
967d0e4836 | |
|
5ccb36af57 | |
|
28f8546ac5 | |
|
0ff0971aca | |
|
dc1091016b | |
|
69bba30557 | |
|
da9bc1237d | |
|
ab11ee4fbe | |
|
466dce8aed | |
|
808dd9d73c | |
|
aef306465d | |
|
7459a4127c | |
|
fc77e6eca5 | |
|
26526b86c3 | |
|
d079675dd4 | |
|
c2acc4f55c | |
|
326b258fd5 | |
|
4f4c7e6b67 | |
|
c05d08e426 | |
|
02062c5dc0 | |
|
72c4a9d20b | |
|
ccc3b1fce1 | |
|
11c4e65757 | |
|
33ac3ca99f | |
|
9ada628a57 | |
|
d2c3e32bf1 | |
|
51944a0b99 | |
|
024e8015da | |
|
aaed3a4a37 | |
|
edffd5e367 | |
|
4ca81e39e6 | |
|
dd7aca539f | |
|
735dc9056a | |
|
e949839edf | |
|
6194ac891c | |
|
6554e324f2 | |
|
076caeb596 | |
|
faa678e209 | |
|
c5d68f6b58 | |
|
506aefb917 | |
|
7436d52f37 | |
|
80b074e3e7 | |
|
e97efb7099 | |
|
81b11fd665 | |
|
aa2b1fbf8b | |
|
82c12253e5 | |
|
7f451409ec | |
|
9be6f6d3e9 | |
|
9d2c7ae3cf | |
|
a81a1be40c | |
|
c85575e6ce | |
|
aa98cbd848 | |
|
a890e9aa83 | |
|
1592f7e6be | |
|
1c9293e69d | |
|
ec13c1b31d | |
|
7ce366097d | |
|
6cedda008a | |
|
207175d78e | |
|
57b5e51099 | |
|
b72c8dce9b | |
|
bfa4d71009 | |
|
434e22680e | |
|
636c19866c |
|
@ -16,7 +16,6 @@ from tractor import (
|
||||||
ContextCancelled,
|
ContextCancelled,
|
||||||
MsgStream,
|
MsgStream,
|
||||||
_testing,
|
_testing,
|
||||||
trionics,
|
|
||||||
)
|
)
|
||||||
import trio
|
import trio
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -63,8 +62,9 @@ async def recv_and_spawn_net_killers(
|
||||||
await ctx.started()
|
await ctx.started()
|
||||||
async with (
|
async with (
|
||||||
ctx.open_stream() as stream,
|
ctx.open_stream() as stream,
|
||||||
trionics.collapse_eg(),
|
trio.open_nursery(
|
||||||
trio.open_nursery() as tn,
|
strict_exception_groups=False,
|
||||||
|
) as tn,
|
||||||
):
|
):
|
||||||
async for i in stream:
|
async for i in stream:
|
||||||
print(f'child echoing {i}')
|
print(f'child echoing {i}')
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
import trio
|
|
||||||
import tractor
|
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
async with tractor.open_root_actor(
|
|
||||||
debug_mode=True,
|
|
||||||
loglevel='cancel',
|
|
||||||
) as _root:
|
|
||||||
|
|
||||||
# manually trigger self-cancellation and wait
|
|
||||||
# for it to fully trigger.
|
|
||||||
_root.cancel_soon()
|
|
||||||
await _root._cancel_complete.wait()
|
|
||||||
print('root cancelled')
|
|
||||||
|
|
||||||
# now ensure we can still use the REPL
|
|
||||||
try:
|
|
||||||
await tractor.pause()
|
|
||||||
except trio.Cancelled as _taskc:
|
|
||||||
assert (root_cs := _root._root_tn.cancel_scope).cancel_called
|
|
||||||
# NOTE^^ above logic but inside `open_root_actor()` and
|
|
||||||
# passed to the `shield=` expression is effectively what
|
|
||||||
# we're testing here!
|
|
||||||
await tractor.pause(shield=root_cs.cancel_called)
|
|
||||||
|
|
||||||
# XXX, if shield logic *is wrong* inside `open_root_actor()`'s
|
|
||||||
# crash-handler block this should never be interacted,
|
|
||||||
# instead `trio.Cancelled` would be bubbled up: the original
|
|
||||||
# BUG.
|
|
||||||
assert 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
trio.run(main)
|
|
|
@ -23,8 +23,9 @@ async def main():
|
||||||
modules=[__name__]
|
modules=[__name__]
|
||||||
) as portal_map,
|
) as portal_map,
|
||||||
|
|
||||||
tractor.trionics.collapse_eg(),
|
trio.open_nursery(
|
||||||
trio.open_nursery() as tn,
|
strict_exception_groups=False,
|
||||||
|
) as tn,
|
||||||
):
|
):
|
||||||
|
|
||||||
for (name, portal) in portal_map.items():
|
for (name, portal) in portal_map.items():
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
"""
|
"""
|
||||||
That "native" debug mode better work!
|
That "native" debug mode better work!
|
||||||
|
|
||||||
All these tests can be understood (somewhat) by running the
|
All these tests can be understood (somewhat) by running the equivalent
|
||||||
equivalent `examples/debugging/` scripts manually.
|
`examples/debugging/` scripts manually.
|
||||||
|
|
||||||
TODO:
|
TODO:
|
||||||
- none of these tests have been run successfully on windows yet but
|
- none of these tests have been run successfully on windows yet but
|
||||||
|
@ -1156,54 +1156,6 @@ def test_ctxep_pauses_n_maybe_ipc_breaks(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_crash_handling_within_cancelled_root_actor(
|
|
||||||
spawn: PexpectSpawner,
|
|
||||||
):
|
|
||||||
'''
|
|
||||||
Ensure that when only a root-actor is started via `open_root_actor()`
|
|
||||||
we can crash-handle in debug-mode despite self-cancellation.
|
|
||||||
|
|
||||||
More-or-less ensures we conditionally shield the pause in
|
|
||||||
`._root.open_root_actor()`'s `await debug._maybe_enter_pm()`
|
|
||||||
call.
|
|
||||||
|
|
||||||
'''
|
|
||||||
child = spawn('root_self_cancelled_w_error')
|
|
||||||
child.expect(PROMPT)
|
|
||||||
|
|
||||||
assert_before(
|
|
||||||
child,
|
|
||||||
[
|
|
||||||
"Actor.cancel_soon()` was called!",
|
|
||||||
"root cancelled",
|
|
||||||
_pause_msg,
|
|
||||||
"('root'", # actor name
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
child.sendline('c')
|
|
||||||
child.expect(PROMPT)
|
|
||||||
assert_before(
|
|
||||||
child,
|
|
||||||
[
|
|
||||||
_crash_msg,
|
|
||||||
"('root'", # actor name
|
|
||||||
"AssertionError",
|
|
||||||
"assert 0",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
child.sendline('c')
|
|
||||||
child.expect(EOF)
|
|
||||||
assert_before(
|
|
||||||
child,
|
|
||||||
[
|
|
||||||
"AssertionError",
|
|
||||||
"assert 0",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# TODO: better error for "non-ideal" usage from the root actor.
|
# TODO: better error for "non-ideal" usage from the root actor.
|
||||||
# -[ ] if called from an async scope emit a message that suggests
|
# -[ ] if called from an async scope emit a message that suggests
|
||||||
# using `await tractor.pause()` instead since it's less overhead
|
# using `await tractor.pause()` instead since it's less overhead
|
||||||
|
|
|
@ -18,9 +18,8 @@ from tractor import (
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def bindspace_dir_str() -> str:
|
def bindspace_dir_str() -> str:
|
||||||
|
|
||||||
rt_dir: Path = tractor._state.get_rt_dir()
|
bs_dir_str: str = '/run/user/1000/doggy'
|
||||||
bs_dir: Path = rt_dir / 'doggy'
|
bs_dir = Path(bs_dir_str)
|
||||||
bs_dir_str: str = str(bs_dir)
|
|
||||||
assert not bs_dir.is_dir()
|
assert not bs_dir.is_dir()
|
||||||
|
|
||||||
yield bs_dir_str
|
yield bs_dir_str
|
||||||
|
|
|
@ -313,8 +313,9 @@ async def inf_streamer(
|
||||||
# `trio.EndOfChannel` doesn't propagate directly to the above
|
# `trio.EndOfChannel` doesn't propagate directly to the above
|
||||||
# .open_stream() parent, resulting in it also raising instead
|
# .open_stream() parent, resulting in it also raising instead
|
||||||
# of gracefully absorbing as normal.. so how to handle?
|
# of gracefully absorbing as normal.. so how to handle?
|
||||||
tractor.trionics.collapse_eg(),
|
trio.open_nursery(
|
||||||
trio.open_nursery() as tn,
|
strict_exception_groups=False,
|
||||||
|
) as tn,
|
||||||
):
|
):
|
||||||
async def close_stream_on_sentinel():
|
async def close_stream_on_sentinel():
|
||||||
async for msg in stream:
|
async for msg in stream:
|
||||||
|
|
|
@ -236,10 +236,7 @@ async def stream_forever():
|
||||||
async def test_cancel_infinite_streamer(start_method):
|
async def test_cancel_infinite_streamer(start_method):
|
||||||
|
|
||||||
# stream for at most 1 seconds
|
# stream for at most 1 seconds
|
||||||
with (
|
with trio.move_on_after(1) as cancel_scope:
|
||||||
trio.fail_after(4),
|
|
||||||
trio.move_on_after(1) as cancel_scope
|
|
||||||
):
|
|
||||||
async with tractor.open_nursery() as n:
|
async with tractor.open_nursery() as n:
|
||||||
portal = await n.start_actor(
|
portal = await n.start_actor(
|
||||||
'donny',
|
'donny',
|
||||||
|
@ -535,15 +532,10 @@ def test_cancel_via_SIGINT_other_task(
|
||||||
async def main():
|
async def main():
|
||||||
# should never timeout since SIGINT should cancel the current program
|
# should never timeout since SIGINT should cancel the current program
|
||||||
with trio.fail_after(timeout):
|
with trio.fail_after(timeout):
|
||||||
async with (
|
async with trio.open_nursery(
|
||||||
|
|
||||||
# XXX ?TODO? why no work!?
|
|
||||||
# tractor.trionics.collapse_eg(),
|
|
||||||
trio.open_nursery(
|
|
||||||
strict_exception_groups=False,
|
strict_exception_groups=False,
|
||||||
) as tn,
|
) as n:
|
||||||
):
|
await n.start(spawn_and_sleep_forever)
|
||||||
await tn.start(spawn_and_sleep_forever)
|
|
||||||
if 'mp' in spawn_backend:
|
if 'mp' in spawn_backend:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
os.kill(pid, signal.SIGINT)
|
os.kill(pid, signal.SIGINT)
|
||||||
|
|
|
@ -117,10 +117,9 @@ async def open_actor_local_nursery(
|
||||||
ctx: tractor.Context,
|
ctx: tractor.Context,
|
||||||
):
|
):
|
||||||
global _nursery
|
global _nursery
|
||||||
async with (
|
async with trio.open_nursery(
|
||||||
tractor.trionics.collapse_eg(),
|
strict_exception_groups=False,
|
||||||
trio.open_nursery() as tn
|
) as tn:
|
||||||
):
|
|
||||||
_nursery = tn
|
_nursery = tn
|
||||||
await ctx.started()
|
await ctx.started()
|
||||||
await trio.sleep(10)
|
await trio.sleep(10)
|
||||||
|
|
|
@ -11,7 +11,6 @@ import psutil
|
||||||
import pytest
|
import pytest
|
||||||
import subprocess
|
import subprocess
|
||||||
import tractor
|
import tractor
|
||||||
from tractor.trionics import collapse_eg
|
|
||||||
from tractor._testing import tractor_test
|
from tractor._testing import tractor_test
|
||||||
import trio
|
import trio
|
||||||
|
|
||||||
|
@ -194,10 +193,10 @@ async def spawn_and_check_registry(
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with tractor.open_nursery() as an:
|
async with tractor.open_nursery() as an:
|
||||||
async with (
|
async with trio.open_nursery(
|
||||||
collapse_eg(),
|
strict_exception_groups=False,
|
||||||
trio.open_nursery() as trion,
|
) as trion:
|
||||||
):
|
|
||||||
portals = {}
|
portals = {}
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
name = f'a{i}'
|
name = f'a{i}'
|
||||||
|
@ -339,12 +338,11 @@ async def close_chans_before_nursery(
|
||||||
async with portal2.open_stream_from(
|
async with portal2.open_stream_from(
|
||||||
stream_forever
|
stream_forever
|
||||||
) as agen2:
|
) as agen2:
|
||||||
async with (
|
async with trio.open_nursery(
|
||||||
collapse_eg(),
|
strict_exception_groups=False,
|
||||||
trio.open_nursery() as tn,
|
) as n:
|
||||||
):
|
n.start_soon(streamer, agen1)
|
||||||
tn.start_soon(streamer, agen1)
|
n.start_soon(cancel, use_signal, .5)
|
||||||
tn.start_soon(cancel, use_signal, .5)
|
|
||||||
try:
|
try:
|
||||||
await streamer(agen2)
|
await streamer(agen2)
|
||||||
finally:
|
finally:
|
||||||
|
|
|
@ -234,8 +234,10 @@ async def trio_ctx(
|
||||||
with trio.fail_after(1 + delay):
|
with trio.fail_after(1 + delay):
|
||||||
try:
|
try:
|
||||||
async with (
|
async with (
|
||||||
tractor.trionics.collapse_eg(),
|
trio.open_nursery(
|
||||||
trio.open_nursery() as tn,
|
# TODO, for new `trio` / py3.13
|
||||||
|
# strict_exception_groups=False,
|
||||||
|
) as tn,
|
||||||
tractor.to_asyncio.open_channel_from(
|
tractor.to_asyncio.open_channel_from(
|
||||||
sleep_and_err,
|
sleep_and_err,
|
||||||
) as (first, chan),
|
) as (first, chan),
|
||||||
|
|
|
@ -8,7 +8,6 @@ from contextlib import (
|
||||||
)
|
)
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from tractor.trionics import collapse_eg
|
|
||||||
import trio
|
import trio
|
||||||
from trio import TaskStatus
|
from trio import TaskStatus
|
||||||
|
|
||||||
|
@ -65,8 +64,9 @@ def test_stashed_child_nursery(use_start_soon):
|
||||||
async def main():
|
async def main():
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
collapse_eg(),
|
trio.open_nursery(
|
||||||
trio.open_nursery() as pn,
|
strict_exception_groups=False,
|
||||||
|
) as pn,
|
||||||
):
|
):
|
||||||
cn = await pn.start(mk_child_nursery)
|
cn = await pn.start(mk_child_nursery)
|
||||||
assert cn
|
assert cn
|
||||||
|
@ -195,8 +195,10 @@ def test_gatherctxs_with_memchan_breaks_multicancelled(
|
||||||
async with (
|
async with (
|
||||||
# XXX should ensure ONLY the KBI
|
# XXX should ensure ONLY the KBI
|
||||||
# is relayed upward
|
# is relayed upward
|
||||||
collapse_eg(),
|
trionics.collapse_eg(),
|
||||||
trio.open_nursery(), # as tn,
|
trio.open_nursery(
|
||||||
|
# strict_exception_groups=False,
|
||||||
|
), # as tn,
|
||||||
|
|
||||||
trionics.gather_contexts([
|
trionics.gather_contexts([
|
||||||
open_memchan(),
|
open_memchan(),
|
||||||
|
|
|
@ -478,14 +478,16 @@ async def open_root_actor(
|
||||||
|
|
||||||
# start runtime in a bg sub-task, yield to caller.
|
# start runtime in a bg sub-task, yield to caller.
|
||||||
async with (
|
async with (
|
||||||
collapse_eg(),
|
collapse_eg(
|
||||||
|
# bp=True,
|
||||||
|
hide_tb=False,
|
||||||
|
),
|
||||||
trio.open_nursery() as root_tn,
|
trio.open_nursery() as root_tn,
|
||||||
|
|
||||||
# ?TODO? finally-footgun below?
|
# XXX, finally-footgun below?
|
||||||
# -> see note on why shielding.
|
# -> see note on why shielding.
|
||||||
# maybe_raise_from_masking_exc(),
|
# maybe_raise_from_masking_exc(),
|
||||||
):
|
):
|
||||||
actor._root_tn = root_tn
|
|
||||||
# `_runtime.async_main()` creates an internal nursery
|
# `_runtime.async_main()` creates an internal nursery
|
||||||
# and blocks here until any underlying actor(-process)
|
# and blocks here until any underlying actor(-process)
|
||||||
# tree has terminated thereby conducting so called
|
# tree has terminated thereby conducting so called
|
||||||
|
@ -529,6 +531,7 @@ async def open_root_actor(
|
||||||
# crashes under cancellation conditions; so
|
# crashes under cancellation conditions; so
|
||||||
# most of them!
|
# most of them!
|
||||||
shield=root_tn.cancel_scope.cancel_called,
|
shield=root_tn.cancel_scope.cancel_called,
|
||||||
|
# ^TODO? write a (debugger) test for this ya?
|
||||||
)
|
)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -568,7 +571,6 @@ async def open_root_actor(
|
||||||
f'{op_nested_actor_repr}'
|
f'{op_nested_actor_repr}'
|
||||||
)
|
)
|
||||||
# XXX, THIS IS A *finally-footgun*!
|
# XXX, THIS IS A *finally-footgun*!
|
||||||
# (also mentioned in with-block above)
|
|
||||||
# -> though already shields iternally it can
|
# -> though already shields iternally it can
|
||||||
# taskc here and mask underlying errors raised in
|
# taskc here and mask underlying errors raised in
|
||||||
# the try-block above?
|
# the try-block above?
|
||||||
|
|
|
@ -403,7 +403,7 @@ async def _errors_relayed_via_ipc(
|
||||||
|
|
||||||
# RPC task bookeeping.
|
# RPC task bookeeping.
|
||||||
# since RPC tasks are scheduled inside a flat
|
# since RPC tasks are scheduled inside a flat
|
||||||
# `Actor._service_tn`, we add "handles" to each such that
|
# `Actor._service_n`, we add "handles" to each such that
|
||||||
# they can be individually ccancelled.
|
# they can be individually ccancelled.
|
||||||
finally:
|
finally:
|
||||||
|
|
||||||
|
@ -481,7 +481,7 @@ async def _invoke(
|
||||||
connected IPC channel.
|
connected IPC channel.
|
||||||
|
|
||||||
This is the core "RPC" `trio.Task` scheduling machinery used to start every
|
This is the core "RPC" `trio.Task` scheduling machinery used to start every
|
||||||
remotely invoked function, normally in `Actor._service_tn: Nursery`.
|
remotely invoked function, normally in `Actor._service_n: Nursery`.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
__tracebackhide__: bool = hide_tb
|
__tracebackhide__: bool = hide_tb
|
||||||
|
@ -970,7 +970,7 @@ async def process_messages(
|
||||||
|
|
||||||
Receive (multiplexed) per-`Channel` RPC requests as msgs from
|
Receive (multiplexed) per-`Channel` RPC requests as msgs from
|
||||||
remote processes; schedule target async funcs as local
|
remote processes; schedule target async funcs as local
|
||||||
`trio.Task`s inside the `Actor._service_tn: Nursery`.
|
`trio.Task`s inside the `Actor._service_n: Nursery`.
|
||||||
|
|
||||||
Depending on msg type, non-`cmd` (task spawning/starting)
|
Depending on msg type, non-`cmd` (task spawning/starting)
|
||||||
request payloads (eg. `started`, `yield`, `return`, `error`)
|
request payloads (eg. `started`, `yield`, `return`, `error`)
|
||||||
|
@ -995,7 +995,7 @@ async def process_messages(
|
||||||
|
|
||||||
'''
|
'''
|
||||||
actor: Actor = _state.current_actor()
|
actor: Actor = _state.current_actor()
|
||||||
assert actor._service_tn # runtime state sanity
|
assert actor._service_n # runtime state sanity
|
||||||
|
|
||||||
# TODO: once `trio` get's an "obvious way" for req/resp we
|
# TODO: once `trio` get's an "obvious way" for req/resp we
|
||||||
# should use it?
|
# should use it?
|
||||||
|
@ -1206,7 +1206,7 @@ async def process_messages(
|
||||||
start_status += '->( scheduling new task..\n'
|
start_status += '->( scheduling new task..\n'
|
||||||
log.runtime(start_status)
|
log.runtime(start_status)
|
||||||
try:
|
try:
|
||||||
ctx: Context = await actor._service_tn.start(
|
ctx: Context = await actor._service_n.start(
|
||||||
partial(
|
partial(
|
||||||
_invoke,
|
_invoke,
|
||||||
actor,
|
actor,
|
||||||
|
@ -1346,7 +1346,7 @@ async def process_messages(
|
||||||
) as err:
|
) as err:
|
||||||
|
|
||||||
if nursery_cancelled_before_task:
|
if nursery_cancelled_before_task:
|
||||||
sn: Nursery = actor._service_tn
|
sn: Nursery = actor._service_n
|
||||||
assert sn and sn.cancel_scope.cancel_called # sanity
|
assert sn and sn.cancel_scope.cancel_called # sanity
|
||||||
log.cancel(
|
log.cancel(
|
||||||
f'Service nursery cancelled before it handled {funcname}'
|
f'Service nursery cancelled before it handled {funcname}'
|
||||||
|
|
|
@ -35,15 +35,6 @@ for running all lower level spawning, supervision and msging layers:
|
||||||
SC-transitive RPC via scheduling of `trio` tasks.
|
SC-transitive RPC via scheduling of `trio` tasks.
|
||||||
- registration of newly spawned actors with the discovery sys.
|
- registration of newly spawned actors with the discovery sys.
|
||||||
|
|
||||||
Glossary:
|
|
||||||
--------
|
|
||||||
- tn: a `trio.Nursery` or "task nursery".
|
|
||||||
- an: an `ActorNursery` or "actor nursery".
|
|
||||||
- root: top/parent-most scope/task/process/actor (or other runtime
|
|
||||||
primitive) in a hierarchical tree.
|
|
||||||
- parent-ish: "higher-up" in the runtime-primitive hierarchy.
|
|
||||||
- child-ish: "lower-down" in the runtime-primitive hierarchy.
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from contextlib import (
|
from contextlib import (
|
||||||
|
@ -85,7 +76,6 @@ from tractor.msg import (
|
||||||
)
|
)
|
||||||
from .trionics import (
|
from .trionics import (
|
||||||
collapse_eg,
|
collapse_eg,
|
||||||
maybe_open_nursery,
|
|
||||||
)
|
)
|
||||||
from .ipc import (
|
from .ipc import (
|
||||||
Channel,
|
Channel,
|
||||||
|
@ -183,11 +173,9 @@ class Actor:
|
||||||
|
|
||||||
msg_buffer_size: int = 2**6
|
msg_buffer_size: int = 2**6
|
||||||
|
|
||||||
# nursery placeholders filled in by `async_main()`,
|
# nursery placeholders filled in by `async_main()` after fork
|
||||||
# - after fork for subactors.
|
_service_n: Nursery|None = None
|
||||||
# - during boot for the root actor.
|
|
||||||
_root_tn: Nursery|None = None
|
|
||||||
_service_tn: Nursery|None = None
|
|
||||||
_ipc_server: _server.IPCServer|None = None
|
_ipc_server: _server.IPCServer|None = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1021,48 +1009,12 @@ class Actor:
|
||||||
the RPC service nursery.
|
the RPC service nursery.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
actor_repr: str = _pformat.nest_from_op(
|
assert self._service_n
|
||||||
input_op='>c(',
|
self._service_n.start_soon(
|
||||||
text=self.pformat(),
|
|
||||||
nest_indent=1,
|
|
||||||
)
|
|
||||||
log.cancel(
|
|
||||||
'Actor.cancel_soon()` was called!\n'
|
|
||||||
f'>> scheduling `Actor.cancel()`\n'
|
|
||||||
f'{actor_repr}'
|
|
||||||
)
|
|
||||||
assert self._service_tn
|
|
||||||
self._service_tn.start_soon(
|
|
||||||
self.cancel,
|
self.cancel,
|
||||||
None, # self cancel all rpc tasks
|
None, # self cancel all rpc tasks
|
||||||
)
|
)
|
||||||
|
|
||||||
# schedule a "canceller task" in the `._root_tn` once the
|
|
||||||
# `._service_tn` is fully shutdown; task waits for child-ish
|
|
||||||
# scopes to fully exit then finally cancels its parent,
|
|
||||||
# root-most, scope.
|
|
||||||
async def cancel_root_tn_after_services():
|
|
||||||
log.runtime(
|
|
||||||
'Waiting on service-tn to cancel..\n'
|
|
||||||
f'c>)\n'
|
|
||||||
f'|_{self._service_tn.cancel_scope!r}\n'
|
|
||||||
)
|
|
||||||
await self._cancel_complete.wait()
|
|
||||||
log.cancel(
|
|
||||||
f'`._service_tn` cancelled\n'
|
|
||||||
f'>c)\n'
|
|
||||||
f'|_{self._service_tn.cancel_scope!r}\n'
|
|
||||||
f'\n'
|
|
||||||
f'>> cancelling `._root_tn`\n'
|
|
||||||
f'c>(\n'
|
|
||||||
f' |_{self._root_tn.cancel_scope!r}\n'
|
|
||||||
)
|
|
||||||
self._root_tn.cancel_scope.cancel()
|
|
||||||
|
|
||||||
self._root_tn.start_soon(
|
|
||||||
cancel_root_tn_after_services
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cancel_complete(self) -> bool:
|
def cancel_complete(self) -> bool:
|
||||||
return self._cancel_complete.is_set()
|
return self._cancel_complete.is_set()
|
||||||
|
@ -1167,8 +1119,8 @@ class Actor:
|
||||||
await ipc_server.wait_for_shutdown()
|
await ipc_server.wait_for_shutdown()
|
||||||
|
|
||||||
# cancel all rpc tasks permanently
|
# cancel all rpc tasks permanently
|
||||||
if self._service_tn:
|
if self._service_n:
|
||||||
self._service_tn.cancel_scope.cancel()
|
self._service_n.cancel_scope.cancel()
|
||||||
|
|
||||||
log_meth(msg)
|
log_meth(msg)
|
||||||
self._cancel_complete.set()
|
self._cancel_complete.set()
|
||||||
|
@ -1305,7 +1257,7 @@ class Actor:
|
||||||
'''
|
'''
|
||||||
Cancel all ongoing RPC tasks owned/spawned for a given
|
Cancel all ongoing RPC tasks owned/spawned for a given
|
||||||
`parent_chan: Channel` or simply all tasks (inside
|
`parent_chan: Channel` or simply all tasks (inside
|
||||||
`._service_tn`) when `parent_chan=None`.
|
`._service_n`) when `parent_chan=None`.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
tasks: dict = self._rpc_tasks
|
tasks: dict = self._rpc_tasks
|
||||||
|
@ -1517,55 +1469,46 @@ async def async_main(
|
||||||
accept_addrs.append(addr.unwrap())
|
accept_addrs.append(addr.unwrap())
|
||||||
|
|
||||||
assert accept_addrs
|
assert accept_addrs
|
||||||
|
# The "root" nursery ensures the channel with the immediate
|
||||||
ya_root_tn: bool = bool(actor._root_tn)
|
# parent is kept alive as a resilient service until
|
||||||
ya_service_tn: bool = bool(actor._service_tn)
|
# cancellation steps have (mostly) occurred in
|
||||||
|
# a deterministic way.
|
||||||
# NOTE, a top-most "root" nursery in each actor-process
|
|
||||||
# enables a lifetime priority for the IPC-channel connection
|
|
||||||
# with a sub-actor's immediate parent. I.e. this connection
|
|
||||||
# is kept alive as a resilient service connection until all
|
|
||||||
# other machinery has exited, cancellation of all
|
|
||||||
# embedded/child scopes have completed. This helps ensure
|
|
||||||
# a deterministic (and thus "graceful")
|
|
||||||
# first-class-supervision style teardown where a parent actor
|
|
||||||
# (vs. say peers) is always the last to be contacted before
|
|
||||||
# disconnect.
|
|
||||||
root_tn: trio.Nursery
|
root_tn: trio.Nursery
|
||||||
async with (
|
async with (
|
||||||
collapse_eg(),
|
collapse_eg(),
|
||||||
maybe_open_nursery(
|
trio.open_nursery() as root_tn,
|
||||||
nursery=actor._root_tn,
|
|
||||||
) as root_tn,
|
|
||||||
):
|
):
|
||||||
if ya_root_tn:
|
# actor._root_n = root_tn
|
||||||
assert root_tn is actor._root_tn
|
# assert actor._root_n
|
||||||
else:
|
|
||||||
actor._root_tn = root_tn
|
|
||||||
|
|
||||||
ipc_server: _server.IPCServer
|
ipc_server: _server.IPCServer
|
||||||
async with (
|
async with (
|
||||||
collapse_eg(),
|
collapse_eg(),
|
||||||
maybe_open_nursery(
|
trio.open_nursery() as service_nursery,
|
||||||
nursery=actor._service_tn,
|
|
||||||
) as service_tn,
|
|
||||||
_server.open_ipc_server(
|
_server.open_ipc_server(
|
||||||
parent_tn=service_tn, # ?TODO, why can't this be the root-tn
|
parent_tn=service_nursery,
|
||||||
stream_handler_tn=service_tn,
|
stream_handler_tn=service_nursery,
|
||||||
) as ipc_server,
|
) as ipc_server,
|
||||||
|
# ) as actor._ipc_server,
|
||||||
|
# ^TODO? prettier?
|
||||||
|
|
||||||
):
|
):
|
||||||
if ya_service_tn:
|
|
||||||
assert service_tn is actor._service_tn
|
|
||||||
else:
|
|
||||||
# This nursery is used to handle all inbound
|
# This nursery is used to handle all inbound
|
||||||
# connections to us such that if the TCP server
|
# connections to us such that if the TCP server
|
||||||
# is killed, connections can continue to process
|
# is killed, connections can continue to process
|
||||||
# in the background until this nursery is cancelled.
|
# in the background until this nursery is cancelled.
|
||||||
actor._service_tn = service_tn
|
actor._service_n = service_nursery
|
||||||
|
|
||||||
# set after allocate
|
|
||||||
actor._ipc_server = ipc_server
|
actor._ipc_server = ipc_server
|
||||||
|
assert (
|
||||||
|
actor._service_n
|
||||||
|
and (
|
||||||
|
actor._service_n
|
||||||
|
is
|
||||||
|
actor._ipc_server._parent_tn
|
||||||
|
is
|
||||||
|
ipc_server._stream_handler_tn
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# load exposed/allowed RPC modules
|
# load exposed/allowed RPC modules
|
||||||
# XXX: do this **after** establishing a channel to the parent
|
# XXX: do this **after** establishing a channel to the parent
|
||||||
|
@ -1591,11 +1534,10 @@ async def async_main(
|
||||||
# - root actor: the ``accept_addr`` passed to this method
|
# - root actor: the ``accept_addr`` passed to this method
|
||||||
|
|
||||||
# TODO: why is this not with the root nursery?
|
# TODO: why is this not with the root nursery?
|
||||||
# - see above that the `._service_tn` is what's used?
|
|
||||||
try:
|
try:
|
||||||
eps: list = await ipc_server.listen_on(
|
eps: list = await ipc_server.listen_on(
|
||||||
accept_addrs=accept_addrs,
|
accept_addrs=accept_addrs,
|
||||||
stream_handler_nursery=service_tn,
|
stream_handler_nursery=service_nursery,
|
||||||
)
|
)
|
||||||
log.runtime(
|
log.runtime(
|
||||||
f'Booted IPC server\n'
|
f'Booted IPC server\n'
|
||||||
|
@ -1603,7 +1545,7 @@ async def async_main(
|
||||||
)
|
)
|
||||||
assert (
|
assert (
|
||||||
(eps[0].listen_tn)
|
(eps[0].listen_tn)
|
||||||
is not service_tn
|
is not service_nursery
|
||||||
)
|
)
|
||||||
|
|
||||||
except OSError as oserr:
|
except OSError as oserr:
|
||||||
|
@ -1765,7 +1707,7 @@ async def async_main(
|
||||||
|
|
||||||
# XXX TODO but hard XXX
|
# XXX TODO but hard XXX
|
||||||
# we can't actually do this bc the debugger uses the
|
# we can't actually do this bc the debugger uses the
|
||||||
# _service_tn to spawn the lock task, BUT, in theory if we had
|
# _service_n to spawn the lock task, BUT, in theory if we had
|
||||||
# the root nursery surround this finally block it might be
|
# the root nursery surround this finally block it might be
|
||||||
# actually possible to debug THIS machinery in the same way
|
# actually possible to debug THIS machinery in the same way
|
||||||
# as user task code?
|
# as user task code?
|
||||||
|
|
|
@ -643,9 +643,8 @@ _shutdown_msg: str = (
|
||||||
'Actor-runtime-shutdown'
|
'Actor-runtime-shutdown'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@acm
|
|
||||||
# @api_frame
|
# @api_frame
|
||||||
|
@acm
|
||||||
async def open_nursery(
|
async def open_nursery(
|
||||||
*, # named params only!
|
*, # named params only!
|
||||||
hide_tb: bool = True,
|
hide_tb: bool = True,
|
||||||
|
|
|
@ -481,12 +481,12 @@ async def _pause(
|
||||||
# we have to figure out how to avoid having the service nursery
|
# we have to figure out how to avoid having the service nursery
|
||||||
# cancel on this task start? I *think* this works below:
|
# cancel on this task start? I *think* this works below:
|
||||||
# ```python
|
# ```python
|
||||||
# actor._service_tn.cancel_scope.shield = shield
|
# actor._service_n.cancel_scope.shield = shield
|
||||||
# ```
|
# ```
|
||||||
# but not entirely sure if that's a sane way to implement it?
|
# but not entirely sure if that's a sane way to implement it?
|
||||||
|
|
||||||
# NOTE currently we spawn the lock request task inside this
|
# NOTE currently we spawn the lock request task inside this
|
||||||
# subactor's global `Actor._service_tn` so that the
|
# subactor's global `Actor._service_n` so that the
|
||||||
# lifetime of the lock-request can outlive the current
|
# lifetime of the lock-request can outlive the current
|
||||||
# `._pause()` scope while the user steps through their
|
# `._pause()` scope while the user steps through their
|
||||||
# application code and when they finally exit the
|
# application code and when they finally exit the
|
||||||
|
@ -510,7 +510,7 @@ async def _pause(
|
||||||
f'|_{task}\n'
|
f'|_{task}\n'
|
||||||
)
|
)
|
||||||
with trio.CancelScope(shield=shield):
|
with trio.CancelScope(shield=shield):
|
||||||
req_ctx: Context = await actor._service_tn.start(
|
req_ctx: Context = await actor._service_n.start(
|
||||||
partial(
|
partial(
|
||||||
request_root_stdio_lock,
|
request_root_stdio_lock,
|
||||||
actor_uid=actor.uid,
|
actor_uid=actor.uid,
|
||||||
|
@ -544,7 +544,7 @@ async def _pause(
|
||||||
_repl_fail_report = None
|
_repl_fail_report = None
|
||||||
|
|
||||||
# when the actor is mid-runtime cancellation the
|
# when the actor is mid-runtime cancellation the
|
||||||
# `Actor._service_tn` might get closed before we can spawn
|
# `Actor._service_n` might get closed before we can spawn
|
||||||
# the request task, so just ignore expected RTE.
|
# the request task, so just ignore expected RTE.
|
||||||
elif (
|
elif (
|
||||||
isinstance(pause_err, RuntimeError)
|
isinstance(pause_err, RuntimeError)
|
||||||
|
@ -561,9 +561,6 @@ async def _pause(
|
||||||
return
|
return
|
||||||
|
|
||||||
elif isinstance(pause_err, trio.Cancelled):
|
elif isinstance(pause_err, trio.Cancelled):
|
||||||
__tracebackhide__: bool = False
|
|
||||||
# XXX, unmask to REPL it.
|
|
||||||
# mk_pdb().set_trace(frame=inspect.currentframe())
|
|
||||||
_repl_fail_report += (
|
_repl_fail_report += (
|
||||||
'You called `tractor.pause()` from an already cancelled scope!\n\n'
|
'You called `tractor.pause()` from an already cancelled scope!\n\n'
|
||||||
'Consider `await tractor.pause(shield=True)` to make it work B)\n'
|
'Consider `await tractor.pause(shield=True)` to make it work B)\n'
|
||||||
|
@ -992,7 +989,7 @@ def pause_from_sync(
|
||||||
# that output and assign the `repl` created above!
|
# that output and assign the `repl` created above!
|
||||||
bg_task, _ = trio.from_thread.run(
|
bg_task, _ = trio.from_thread.run(
|
||||||
afn=partial(
|
afn=partial(
|
||||||
actor._service_tn.start,
|
actor._service_n.start,
|
||||||
partial(
|
partial(
|
||||||
_pause_from_bg_root_thread,
|
_pause_from_bg_root_thread,
|
||||||
behalf_of_thread=thread,
|
behalf_of_thread=thread,
|
||||||
|
|
|
@ -17,38 +17,20 @@
|
||||||
Utils to tame mp non-SC madeness
|
Utils to tame mp non-SC madeness
|
||||||
|
|
||||||
'''
|
'''
|
||||||
import platform
|
|
||||||
|
|
||||||
|
|
||||||
|
# !TODO! in 3.13 this can be disabled (the-same/similarly) using
|
||||||
|
# a flag,
|
||||||
|
# - [ ] soo if it works like this, drop this module entirely for
|
||||||
|
# 3.13+ B)
|
||||||
|
# |_https://docs.python.org/3/library/multiprocessing.shared_memory.html
|
||||||
|
#
|
||||||
def disable_mantracker():
|
def disable_mantracker():
|
||||||
'''
|
'''
|
||||||
Disable all `multiprocessing` "resource tracking" machinery since
|
Disable all `multiprocessing` "resource tracking" machinery since
|
||||||
it's an absolute multi-threaded mess of non-SC madness.
|
it's an absolute multi-threaded mess of non-SC madness.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
from multiprocessing.shared_memory import SharedMemory
|
from multiprocessing import resource_tracker as mantracker
|
||||||
|
|
||||||
|
|
||||||
# 3.13+ only.. can pass `track=False` to disable
|
|
||||||
# all the resource tracker bs.
|
|
||||||
# https://docs.python.org/3/library/multiprocessing.shared_memory.html
|
|
||||||
if (_py_313 := (
|
|
||||||
platform.python_version_tuple()[:-1]
|
|
||||||
>=
|
|
||||||
('3', '13')
|
|
||||||
)
|
|
||||||
):
|
|
||||||
from functools import partial
|
|
||||||
return partial(
|
|
||||||
SharedMemory,
|
|
||||||
track=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
# !TODO, once we drop 3.12- we can obvi remove all this!
|
|
||||||
else:
|
|
||||||
from multiprocessing import (
|
|
||||||
resource_tracker as mantracker,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Tell the "resource tracker" thing to fuck off.
|
# Tell the "resource tracker" thing to fuck off.
|
||||||
class ManTracker(mantracker.ResourceTracker):
|
class ManTracker(mantracker.ResourceTracker):
|
||||||
|
@ -68,8 +50,3 @@ def disable_mantracker():
|
||||||
mantracker.ensure_running = mantracker._resource_tracker.ensure_running
|
mantracker.ensure_running = mantracker._resource_tracker.ensure_running
|
||||||
mantracker.unregister = mantracker._resource_tracker.unregister
|
mantracker.unregister = mantracker._resource_tracker.unregister
|
||||||
mantracker.getfd = mantracker._resource_tracker.getfd
|
mantracker.getfd = mantracker._resource_tracker.getfd
|
||||||
|
|
||||||
# use std type verbatim
|
|
||||||
shmT = SharedMemory
|
|
||||||
|
|
||||||
return shmT
|
|
||||||
|
|
|
@ -1001,11 +1001,7 @@ class Server(Struct):
|
||||||
partial(
|
partial(
|
||||||
_serve_ipc_eps,
|
_serve_ipc_eps,
|
||||||
server=self,
|
server=self,
|
||||||
stream_handler_tn=(
|
stream_handler_tn=stream_handler_nursery,
|
||||||
stream_handler_nursery
|
|
||||||
or
|
|
||||||
self._stream_handler_tn
|
|
||||||
),
|
|
||||||
listen_addrs=accept_addrs,
|
listen_addrs=accept_addrs,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -1149,17 +1145,13 @@ async def open_ipc_server(
|
||||||
|
|
||||||
async with maybe_open_nursery(
|
async with maybe_open_nursery(
|
||||||
nursery=parent_tn,
|
nursery=parent_tn,
|
||||||
) as parent_tn:
|
) as rent_tn:
|
||||||
no_more_peers = trio.Event()
|
no_more_peers = trio.Event()
|
||||||
no_more_peers.set()
|
no_more_peers.set()
|
||||||
|
|
||||||
ipc_server = IPCServer(
|
ipc_server = IPCServer(
|
||||||
_parent_tn=parent_tn,
|
_parent_tn=rent_tn,
|
||||||
_stream_handler_tn=(
|
_stream_handler_tn=stream_handler_tn or rent_tn,
|
||||||
stream_handler_tn
|
|
||||||
or
|
|
||||||
parent_tn
|
|
||||||
),
|
|
||||||
_no_more_peers=no_more_peers,
|
_no_more_peers=no_more_peers,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -23,15 +23,14 @@ considered optional within the context of this runtime-library.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from multiprocessing import shared_memory as shm
|
|
||||||
from multiprocessing.shared_memory import (
|
|
||||||
# SharedMemory,
|
|
||||||
ShareableList,
|
|
||||||
)
|
|
||||||
import platform
|
|
||||||
from sys import byteorder
|
from sys import byteorder
|
||||||
import time
|
import time
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from multiprocessing import shared_memory as shm
|
||||||
|
from multiprocessing.shared_memory import (
|
||||||
|
SharedMemory,
|
||||||
|
ShareableList,
|
||||||
|
)
|
||||||
|
|
||||||
from msgspec import (
|
from msgspec import (
|
||||||
Struct,
|
Struct,
|
||||||
|
@ -62,7 +61,7 @@ except ImportError:
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
SharedMemory = disable_mantracker()
|
disable_mantracker()
|
||||||
|
|
||||||
|
|
||||||
class SharedInt:
|
class SharedInt:
|
||||||
|
@ -798,14 +797,7 @@ def open_shm_list(
|
||||||
# "close" attached shm on actor teardown
|
# "close" attached shm on actor teardown
|
||||||
try:
|
try:
|
||||||
actor = tractor.current_actor()
|
actor = tractor.current_actor()
|
||||||
|
|
||||||
actor.lifetime_stack.callback(shml.shm.close)
|
actor.lifetime_stack.callback(shml.shm.close)
|
||||||
|
|
||||||
# XXX on 3.13+ we don't need to call this?
|
|
||||||
# -> bc we pass `track=False` for `SharedMemeory` orr?
|
|
||||||
if (
|
|
||||||
platform.python_version_tuple()[:-1] < ('3', '13')
|
|
||||||
):
|
|
||||||
actor.lifetime_stack.callback(shml.shm.unlink)
|
actor.lifetime_stack.callback(shml.shm.unlink)
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
log.warning('tractor runtime not active, skipping teardown steps')
|
log.warning('tractor runtime not active, skipping teardown steps')
|
||||||
|
|
|
@ -215,7 +215,7 @@ class LinkedTaskChannel(
|
||||||
val: Any = None,
|
val: Any = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Synchronize aio-side with its trio-parent.
|
Synchronize aio-sde with its trio-parent.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
self._aio_started_val = val
|
self._aio_started_val = val
|
||||||
|
@ -459,22 +459,14 @@ def _run_asyncio_task(
|
||||||
f'Task exited with final result: {result!r}\n'
|
f'Task exited with final result: {result!r}\n'
|
||||||
)
|
)
|
||||||
|
|
||||||
# XXX ALWAYS close the child-`asyncio`-task-side's
|
# only close the aio (child) side which will relay
|
||||||
# `to_trio` handle which will in turn relay
|
# a `trio.EndOfChannel` to the trio (parent) side.
|
||||||
# a `trio.EndOfChannel` to the `trio`-parent.
|
|
||||||
# Consequently the parent `trio` task MUST ALWAYS
|
|
||||||
# check for any `chan._aio_err` to be raised when it
|
|
||||||
# receives an EoC.
|
|
||||||
#
|
|
||||||
# NOTE, there are 2 EoC cases,
|
|
||||||
# - normal/graceful EoC due to the aio-side actually
|
|
||||||
# terminating its "streaming", but the task did not
|
|
||||||
# error and is not yet complete.
|
|
||||||
#
|
|
||||||
# - the aio-task terminated and we specially mark the
|
|
||||||
# closure as due to the `asyncio.Task`'s exit.
|
|
||||||
#
|
#
|
||||||
|
# XXX NOTE, that trio-side MUST then in such cases
|
||||||
|
# check for a `chan._aio_err` and raise it!!
|
||||||
to_trio.close()
|
to_trio.close()
|
||||||
|
# specially mark the closure as due to the
|
||||||
|
# asyncio.Task terminating!
|
||||||
chan._closed_by_aio_task = True
|
chan._closed_by_aio_task = True
|
||||||
|
|
||||||
aio_task_complete.set()
|
aio_task_complete.set()
|
||||||
|
@ -854,6 +846,8 @@ async def translate_aio_errors(
|
||||||
chan._trio_to_raise = aio_err
|
chan._trio_to_raise = aio_err
|
||||||
trio_err = chan._trio_err = eoc
|
trio_err = chan._trio_err = eoc
|
||||||
#
|
#
|
||||||
|
# await tractor.pause(shield=True)
|
||||||
|
#
|
||||||
# ?TODO?, raise something like a,
|
# ?TODO?, raise something like a,
|
||||||
# chan._trio_to_raise = AsyncioErrored()
|
# chan._trio_to_raise = AsyncioErrored()
|
||||||
# BUT, with the tb rewritten to reflect the underlying
|
# BUT, with the tb rewritten to reflect the underlying
|
||||||
|
|
|
@ -31,6 +31,7 @@ from typing import (
|
||||||
AsyncIterator,
|
AsyncIterator,
|
||||||
Callable,
|
Callable,
|
||||||
Hashable,
|
Hashable,
|
||||||
|
Optional,
|
||||||
Sequence,
|
Sequence,
|
||||||
TypeVar,
|
TypeVar,
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
|
@ -203,7 +204,7 @@ class _Cache:
|
||||||
a kept-alive-while-in-use async resource.
|
a kept-alive-while-in-use async resource.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
service_tn: trio.Nursery|None = None
|
service_n: Optional[trio.Nursery] = None
|
||||||
locks: dict[Hashable, trio.Lock] = {}
|
locks: dict[Hashable, trio.Lock] = {}
|
||||||
users: int = 0
|
users: int = 0
|
||||||
values: dict[Any, Any] = {}
|
values: dict[Any, Any] = {}
|
||||||
|
@ -212,7 +213,7 @@ class _Cache:
|
||||||
tuple[trio.Nursery, trio.Event]
|
tuple[trio.Nursery, trio.Event]
|
||||||
] = {}
|
] = {}
|
||||||
# nurseries: dict[int, trio.Nursery] = {}
|
# nurseries: dict[int, trio.Nursery] = {}
|
||||||
no_more_users: trio.Event|None = None
|
no_more_users: Optional[trio.Event] = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def run_ctx(
|
async def run_ctx(
|
||||||
|
@ -295,15 +296,15 @@ async def maybe_open_context(
|
||||||
f'task: {task}\n'
|
f'task: {task}\n'
|
||||||
f'task_tn: {task_tn}\n'
|
f'task_tn: {task_tn}\n'
|
||||||
)
|
)
|
||||||
service_tn = tn
|
service_n = tn
|
||||||
else:
|
else:
|
||||||
service_tn: trio.Nursery = current_actor()._service_tn
|
service_n: trio.Nursery = current_actor()._service_n
|
||||||
|
|
||||||
# TODO: is there any way to allocate
|
# TODO: is there any way to allocate
|
||||||
# a 'stays-open-till-last-task-finshed nursery?
|
# a 'stays-open-till-last-task-finshed nursery?
|
||||||
# service_tn: trio.Nursery
|
# service_n: trio.Nursery
|
||||||
# async with maybe_open_nursery(_Cache.service_tn) as service_tn:
|
# async with maybe_open_nursery(_Cache.service_n) as service_n:
|
||||||
# _Cache.service_tn = service_tn
|
# _Cache.service_n = service_n
|
||||||
|
|
||||||
cache_miss_ke: KeyError|None = None
|
cache_miss_ke: KeyError|None = None
|
||||||
maybe_taskc: trio.Cancelled|None = None
|
maybe_taskc: trio.Cancelled|None = None
|
||||||
|
@ -325,8 +326,8 @@ async def maybe_open_context(
|
||||||
mngr = acm_func(**kwargs)
|
mngr = acm_func(**kwargs)
|
||||||
resources = _Cache.resources
|
resources = _Cache.resources
|
||||||
assert not resources.get(ctx_key), f'Resource exists? {ctx_key}'
|
assert not resources.get(ctx_key), f'Resource exists? {ctx_key}'
|
||||||
resources[ctx_key] = (service_tn, trio.Event())
|
resources[ctx_key] = (service_n, trio.Event())
|
||||||
yielded: Any = await service_tn.start(
|
yielded: Any = await service_n.start(
|
||||||
_Cache.run_ctx,
|
_Cache.run_ctx,
|
||||||
mngr,
|
mngr,
|
||||||
ctx_key,
|
ctx_key,
|
||||||
|
|
Loading…
Reference in New Issue