|
22 | 22 |
|
23 | 23 | __all__ = ("CancellationToken", "Scheduler") |
24 | 24 |
|
25 | | -MISSING: t.Any = object() |
26 | | - |
27 | 25 |
|
28 | 26 | class CancellationToken: |
29 | 27 | """An object to use for cancelation of a task. |
@@ -72,34 +70,17 @@ def __init_subclass__(cls) -> t.Never: |
72 | 70 |
|
73 | 71 | __final__ = True |
74 | 72 |
|
75 | | - __tasks: dict[CancellationToken, _Task[T]] |
76 | | - __tqueue: asyncio.PriorityQueue[_Task[T]] |
77 | | - __closed: bool |
78 | | - __l: asyncio.Lock |
79 | | - __granularity: float |
80 | | - |
81 | 73 | __slots__ = ("__closed", "__granularity", "__l", "__tasks", "__tqueue") |
82 | 74 |
|
83 | 75 | def __init__(self, granularity: float, /) -> None: |
84 | | - self.__granularity = granularity |
85 | | - self.__closed = MISSING |
86 | | - self.__tasks = MISSING |
87 | | - self.__tqueue = MISSING |
88 | | - self.__l = MISSING |
89 | | - |
90 | | - async def __aenter__(self) -> t.Self: |
91 | | - self.__closed = False |
92 | | - asyncio.get_running_loop() |
93 | | - |
94 | | - # lock is only needeed on modifying or removing tasks |
95 | | - # insertion is not guarded and only racy in the order of emitted events |
96 | | - # when inserting a task that is scheduled in the past |
97 | | - # or within 1 full iteration of pending tasks on the event loop |
98 | | - # (generally, ms range (subsecond), depending on application) |
| 76 | + self.__granularity: float = granularity |
| 77 | + self.__closed: bool = False |
| 78 | + self.__tasks: dict[CancellationToken, _Task[T]] = {} |
| 79 | + # PYUPGRADE: check: 3.15; relies on asyncio.Lock & Queues not eagerly binding to an event loop. |
99 | 80 | self.__l = asyncio.Lock() |
100 | | - self.__tasks = {} |
101 | | - self.__tqueue = asyncio.PriorityQueue() |
| 81 | + self.__tqueue: asyncio.PriorityQueue[_Task[T]] = asyncio.PriorityQueue() |
102 | 82 |
|
| 83 | + async def __aenter__(self) -> t.Self: |
103 | 84 | return self |
104 | 85 |
|
105 | 86 | async def __aexit__(self, *_dont_care: object) -> None: |
|
0 commit comments