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 aed71dc
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 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 @@ -102,22 +101,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 +130,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 +150,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 +203,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 +256,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 +285,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 +304,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 aed71dc

Please sign in to comment.