Skip to content

Commit

Permalink
Go all in on "task manager" naming
Browse files Browse the repository at this point in the history
  • Loading branch information
goodboy committed May 19, 2023
1 parent 940e65f commit 0025e09
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions tractor/trionics/_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from contextlib import (
asynccontextmanager as acm,
contextmanager as cm,
nullcontext,
)
from typing import (
Generator,
Expand Down Expand Up @@ -51,7 +50,7 @@ class TaskOutcome(Struct):
'''
lowlevel_task: Task
_exited: Event = trio.Event() # as per `trio.Runner.task_exited()`
_exited: trio.Event = trio.Event() # as per `trio.Runner.task_exited()`
_outcome: Outcome | None = None # as per `outcome.Outcome`
_result: Any | None = None # the eventual maybe-returned-value

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

Expand Down Expand Up @@ -102,22 +100,22 @@ async def wait_for_result(self) -> Any:
return self.result


class ScopePerTaskNursery(Struct):
class TaskManagerNursery(Struct):
_n: Nursery
_scopes: dict[
Task,
tuple[CancelScope, Outcome]
] = {}

scope_manager: Generator[Any, Outcome, None] | None = None
task_manager: Generator[Any, Outcome, None] | None = None

async def start_soon(
self,
async_fn,
*args,

name=None,
scope_manager: ContextManager | None = None,
task_manager: Generator[Any, Outcome, None] | None = None

) -> tuple[CancelScope, Task]:

Expand All @@ -131,7 +129,7 @@ async def start_soon(

n: Nursery = self._n

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

) -> None:

# TODO: this was working before?!
# TODO: this was working before?! and, do we need something
# like it to implement `.start()`?
# nonlocal to_return

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

# TODO: is this the only way we can have a per-task scope
# allocated or can we allow the user to somehow do it if
# they want below?
# scope: CancelScope,

) -> Generator[
Any,
Outcome,
Expand Down Expand Up @@ -261,14 +255,11 @@ def add_task_handle_and_crash_handling(

@acm
async def open_nursery(
scope_manager = None,
task_manager = None,
**kwargs,
):
async with trio.open_nursery(**kwargs) as nurse:
yield ScopePerTaskNursery(
nurse,
scope_manager=scope_manager,
)
yield TaskManagerNursery(nurse, task_manager=task_manager)


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

async def main():
async with open_nursery(
scope_manager=add_task_handle_and_crash_handling,
task_manager=add_task_handle_and_crash_handling,
) as sn:
for _ in range(3):
outcome, _ = await sn.start_soon(trio.sleep_forever)
Expand All @@ -312,7 +303,7 @@ async def main():

await trio.sleep(0.6)
print(
'Cancelling and waiting on {err_outcome.lowlevel_task} '
f'Cancelling and waiting on {err_outcome.lowlevel_task} '
'to CRASH..'
)
cs.cancel()
Expand Down

0 comments on commit 0025e09

Please sign in to comment.