From 2815f1c343783ffaeddc06ca9b9e0e947e88163f Mon Sep 17 00:00:00 2001 From: overclockworked64 Date: Sat, 16 Oct 2021 17:34:59 +0200 Subject: [PATCH] Make 'async_enter_all' take a teardown trigger which '_enter_and_wait' will wait on --- tractor/trionics/_mngrs.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tractor/trionics/_mngrs.py b/tractor/trionics/_mngrs.py index 09d4b14..878bc35 100644 --- a/tractor/trionics/_mngrs.py +++ b/tractor/trionics/_mngrs.py @@ -13,12 +13,12 @@ import trio T = TypeVar("T") -async def _enter_and_sleep( +async def _enter_and_wait( mngr: AsyncContextManager[T], to_yield: dict[int, T], all_entered: trio.Event, - # task_status: TaskStatus[T] = trio.TASK_STATUS_IGNORED, + teardown_trigger: trio.Event, ) -> T: '''Open the async context manager deliver it's value @@ -31,14 +31,14 @@ async def _enter_and_sleep( if all(to_yield.values()): all_entered.set() - # sleep until cancelled - await trio.sleep_forever() + await teardown_trigger.wait() @acm async def async_enter_all( *mngrs: tuple[AsyncContextManager[T]], + teardown_trigger: trio.Event, ) -> tuple[T]: @@ -49,16 +49,13 @@ async def async_enter_all( async with trio.open_nursery() as n: for mngr in mngrs: n.start_soon( - _enter_and_sleep, + _enter_and_wait, mngr, to_yield, all_entered, + teardown_trigger, ) # deliver control once all managers have started up await all_entered.wait() yield tuple(to_yield.values()) - - # tear down all sleeper tasks thus triggering individual - # mngr ``__aexit__()``s. - n.cancel_scope.cancel()