Skip to content

Commit 37a36ec

Browse files
committed
Add a new .trionics._tn for "task nursery stuff"
Since I'd like to decouple the new "task-manager-nursery" lowlevel primitives/abstractions from the higher-level `TaskManagerNursery`-supporting API(s) and default per-task supervision-strat and because `._mngr` is already purposed for higher-level "on-top-of-nursery" patterns as it is. Deats, - move `maybe_open_nursery()` into the new mod. - adjust the pkg-mod's import to the new sub-mod. - also draft up this idea for an API which stacks `._beg.collapse_eg()` onto a nursery with the WIP name `open_loose_tn()` but more then likely i'll just discard this idea bc i think the explicit `@acm` stacking is more explicit/pythonic/up-front-grokable despite the extra LoC.
1 parent da79f1f commit 37a36ec

3 files changed

Lines changed: 98 additions & 30 deletions

File tree

tractor/trionics/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from ._mngrs import (
2222
gather_contexts as gather_contexts,
2323
maybe_open_context as maybe_open_context,
24-
maybe_open_nursery as maybe_open_nursery,
2524
)
2625
from ._broadcast import (
2726
AsyncReceiver as AsyncReceiver,
@@ -37,3 +36,6 @@
3736
from ._taskc import (
3837
maybe_raise_from_masking_exc as maybe_raise_from_masking_exc,
3938
)
39+
from ._tn import (
40+
maybe_open_nursery as maybe_open_nursery,
41+
)

tractor/trionics/_mngrs.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
asynccontextmanager as acm,
2424
)
2525
import inspect
26-
from types import ModuleType
2726
from typing import (
2827
Any,
2928
AsyncContextManager,
@@ -33,52 +32,25 @@
3332
Hashable,
3433
Sequence,
3534
TypeVar,
36-
TYPE_CHECKING,
3735
)
3836

3937
import trio
4038
from tractor._state import current_actor
4139
from tractor.log import get_logger
40+
from ._tn import maybe_open_nursery
4241
# from ._beg import collapse_eg
4342
# from ._taskc import (
4443
# maybe_raise_from_masking_exc,
4544
# )
4645

4746

48-
if TYPE_CHECKING:
49-
from tractor import ActorNursery
50-
5147

5248
log = get_logger()
5349

5450
# A regular invariant generic type
5551
T = TypeVar("T")
5652

5753

58-
@acm
59-
async def maybe_open_nursery(
60-
nursery: trio.Nursery|ActorNursery|None = None,
61-
shield: bool = False,
62-
lib: ModuleType = trio,
63-
64-
**kwargs, # proxy thru
65-
66-
) -> AsyncGenerator[trio.Nursery, Any]:
67-
'''
68-
Create a new nursery if None provided.
69-
70-
Blocks on exit as expected if no input nursery is provided.
71-
72-
'''
73-
if nursery is not None:
74-
yield nursery
75-
else:
76-
async with lib.open_nursery(**kwargs) as nursery:
77-
if lib == trio:
78-
nursery.cancel_scope.shield = shield
79-
yield nursery
80-
81-
8254
async def _enter_and_wait(
8355
mngr: AsyncContextManager[T],
8456
unwrapped: dict[int, T],

tractor/trionics/_tn.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# tractor: structured concurrent "actors".
2+
# Copyright 2018-eternity Tyler Goodlet.
3+
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU Affero General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU Affero General Public License for more details.
13+
14+
# You should have received a copy of the GNU Affero General Public License
15+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
'''
18+
`trio.Nursery` wrappers which we short-hand refer to as
19+
`tn`: "task nursery".
20+
21+
(whereas we refer to `tractor.ActorNursery` as the short-hand `an`)
22+
23+
'''
24+
from __future__ import annotations
25+
from contextlib import (
26+
asynccontextmanager as acm,
27+
)
28+
from types import ModuleType
29+
from typing import (
30+
Any,
31+
AsyncGenerator,
32+
TYPE_CHECKING,
33+
)
34+
35+
import trio
36+
from tractor.log import get_logger
37+
38+
# from ._beg import (
39+
# collapse_eg,
40+
# )
41+
42+
if TYPE_CHECKING:
43+
from tractor import ActorNursery
44+
45+
log = get_logger(__name__)
46+
47+
48+
# ??TODO? is this even a good idea??
49+
# it's an extra LoC to stack `collapse_eg()` vs.
50+
# a new/foreign/bad-std-named very thing wrapper..?
51+
# -[ ] is there a better/simpler name?
52+
# @acm
53+
# async def open_loose_tn() -> trio.Nursery:
54+
# '''
55+
# Implements the equivalent of the old style loose eg raising
56+
# task-nursery from `trio<=0.25.0` ,
57+
58+
# .. code-block:: python
59+
60+
# async with trio.open_nursery(
61+
# strict_exception_groups=False,
62+
# ) as tn:
63+
# ...
64+
65+
# '''
66+
# async with (
67+
# collapse_eg(),
68+
# trio.open_nursery() as tn,
69+
# ):
70+
# yield tn
71+
72+
73+
@acm
74+
async def maybe_open_nursery(
75+
nursery: trio.Nursery|ActorNursery|None = None,
76+
shield: bool = False,
77+
lib: ModuleType = trio,
78+
loose: bool = False,
79+
80+
**kwargs, # proxy thru
81+
82+
) -> AsyncGenerator[trio.Nursery, Any]:
83+
'''
84+
Create a new nursery if None provided.
85+
86+
Blocks on exit as expected if no input nursery is provided.
87+
88+
'''
89+
if nursery is not None:
90+
yield nursery
91+
else:
92+
async with lib.open_nursery(**kwargs) as tn:
93+
tn.cancel_scope.shield = shield
94+
yield tn

0 commit comments

Comments
 (0)