Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e0c888f

Browse files
committedMay 19, 2023
Go all in on "task manager" naming
1 parent 940e65f commit e0c888f

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed
 

‎tractor/trionics/_supervisor.py

+14-23
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from contextlib import (
2323
asynccontextmanager as acm,
2424
contextmanager as cm,
25-
nullcontext,
2625
)
2726
from typing import (
2827
Generator,
@@ -51,7 +50,7 @@ class TaskOutcome(Struct):
5150
5251
'''
5352
lowlevel_task: Task
54-
_exited: Event = trio.Event() # as per `trio.Runner.task_exited()`
53+
_exited = trio.Event() # as per `trio.Runner.task_exited()`
5554
_outcome: Outcome | None = None # as per `outcome.Outcome`
5655
_result: Any | None = None # the eventual maybe-returned-value
5756

@@ -63,9 +62,8 @@ def result(self) -> Any:
6362
'''
6463
if self._outcome is None:
6564
raise RuntimeError(
66-
# f'Task {task.name} is not complete.\n'
67-
f'Outcome is not complete.\n'
68-
'wait on `await TaskOutcome.wait_for_result()` first!'
65+
f'Task {self.lowlevel_task.name} is not complete.\n'
66+
'First wait on `await TaskOutcome.wait_for_result()`!'
6967
)
7068
return self._result
7169

@@ -102,22 +100,22 @@ async def wait_for_result(self) -> Any:
102100
return self.result
103101

104102

105-
class ScopePerTaskNursery(Struct):
103+
class TaskManagerNursery(Struct):
106104
_n: Nursery
107105
_scopes: dict[
108106
Task,
109107
tuple[CancelScope, Outcome]
110108
] = {}
111109

112-
scope_manager: Generator[Any, Outcome, None] | None = None
110+
task_manager: Generator[Any, Outcome, None] | None = None
113111

114112
async def start_soon(
115113
self,
116114
async_fn,
117115
*args,
118116

119117
name=None,
120-
scope_manager: ContextManager | None = None,
118+
task_manager: Generator[Any, Outcome, None] | None = None
121119

122120
) -> tuple[CancelScope, Task]:
123121

@@ -131,7 +129,7 @@ async def start_soon(
131129

132130
n: Nursery = self._n
133131

134-
sm = self.scope_manager
132+
sm = self.task_manager
135133
# we do default behavior of a scope-per-nursery
136134
# if the user did not provide a task manager.
137135
if sm is None:
@@ -151,7 +149,8 @@ async def _start_wrapped_in_scope(
151149

152150
) -> None:
153151

154-
# TODO: this was working before?!
152+
# TODO: this was working before?! and, do we need something
153+
# like it to implement `.start()`?
155154
# nonlocal to_return
156155

157156
# execute up to the first yield
@@ -203,15 +202,10 @@ async def _start_wrapped_in_scope(
203202
# TODO: define a decorator to runtime type check that this a generator
204203
# with a single yield that also delivers a value (of some std type) from
205204
# the yield expression?
206-
# @trio.task_scope_manager
205+
# @trio.task_manager
207206
def add_task_handle_and_crash_handling(
208207
nursery: Nursery,
209208

210-
# TODO: is this the only way we can have a per-task scope
211-
# allocated or can we allow the user to somehow do it if
212-
# they want below?
213-
# scope: CancelScope,
214-
215209
) -> Generator[
216210
Any,
217211
Outcome,
@@ -261,14 +255,11 @@ def add_task_handle_and_crash_handling(
261255

262256
@acm
263257
async def open_nursery(
264-
scope_manager = None,
258+
task_manager = None,
265259
**kwargs,
266260
):
267261
async with trio.open_nursery(**kwargs) as nurse:
268-
yield ScopePerTaskNursery(
269-
nurse,
270-
scope_manager=scope_manager,
271-
)
262+
yield TaskManagerNursery(nurse, task_manager=task_manager)
272263

273264

274265
async def sleep_then_return_val(val: str):
@@ -293,7 +284,7 @@ async def ensure_cancelled():
293284

294285
async def main():
295286
async with open_nursery(
296-
scope_manager=add_task_handle_and_crash_handling,
287+
task_manager=add_task_handle_and_crash_handling,
297288
) as sn:
298289
for _ in range(3):
299290
outcome, _ = await sn.start_soon(trio.sleep_forever)
@@ -312,7 +303,7 @@ async def main():
312303

313304
await trio.sleep(0.6)
314305
print(
315-
'Cancelling and waiting on {err_outcome.lowlevel_task} '
306+
f'Cancelling and waiting on {err_outcome.lowlevel_task} '
316307
'to CRASH..'
317308
)
318309
cs.cancel()

0 commit comments

Comments
 (0)
Please sign in to comment.