- commit
- d0bc4771606dc88db648aaa06bd70d87c8929a84
- parent
- b34bac8ed1f750b2fb8db0a0358826d0593a7b90
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2026-04-16 20:37
TaskGroup: refactor also removes one cancellation point
Diffstat
| M | xiio/core.py | 13 | ++++++++----- |
| M | xiio/multiplex.py | 17 | ++++++----------- |
2 files changed, 14 insertions, 16 deletions
diff --git a/xiio/core.py b/xiio/core.py
@@ -77,9 +77,10 @@ class ThrowCondition(Condition): 77 77 self.exc = exc 78 78 79 7980 -1 class GetTaskCondition(Condition):81 -1 def __init__(self):82 -1 super().__init__(time=-math.inf)-1 80 class SwitchGenCondition(Condition): -1 81 def __init__(self, gen: Gen): -1 82 super().__init__() -1 83 self.gen = gen 83 84 84 85 85 86 async def sleep(seconds: float) -> None: @@ -148,8 +149,10 @@ class Task(typing.Generic[T]): 148 149 elif self.condition.fulfilled(state): 149 150 self._condition = self.gen.send(state) 150 151151 -1 while isinstance(self._condition, GetTaskCondition):152 -1 self._condition = self.gen.send(typing.cast(Files, self))-1 152 while isinstance(self._condition, SwitchGenCondition): -1 153 gen = self.gen -1 154 self.gen = self._condition.gen -1 155 self._condition = gen.send(typing.cast(Files, gen)) 153 156 154 157 if isinstance(self._condition, ThrowCondition): 155 158 exc = self._condition.exc
diff --git a/xiio/multiplex.py b/xiio/multiplex.py
@@ -7,7 +7,7 @@ from .core import CancelledError 7 7 from .core import Condition 8 8 from .core import Coro 9 9 from .core import Gen10 -1 from .core import GetTaskCondition-1 10 from .core import SwitchGenCondition 11 11 from .core import Task 12 12 from .core import ThrowCondition 13 13 from .core import sleep @@ -53,20 +53,15 @@ class TaskGroup(typing.Generic[T]): 53 53 self.cancel(e) 54 54 55 55 async def __aenter__(self) -> 'TaskGroup[T]':56 -1 parent_task = typing.cast(Task[T], await GetTaskCondition())57 -1 gen = parent_task.gen58 -159 56 async def wrapper():60 -1 await Condition(time=-math.inf)61 57 await self62 -1 parent_task.gen = gen63 -1 parent_task._condition = None-1 58 await SwitchGenCondition(parent_gen) 64 59 await Condition(time=-math.inf) 65 6066 -1 self.tasks.append(Task(gen))67 -1 parent_task.gen = typing.cast(typing.Any, wrapper().__await__())68 -1 next(parent_task.gen)69 -1 await Condition(time=-math.inf)-1 61 wrapper_gen = typing.cast(Gen, wrapper().__await__()) -1 62 parent_gen = typing.cast(Gen, await SwitchGenCondition(wrapper_gen)) -1 63 self.tasks.append(Task(parent_gen)) -1 64 await next(wrapper_gen) 70 65 71 66 return self 72 67