|
19 | 19 | import contextvars |
20 | 20 | import heapq |
21 | 21 | import threading |
22 | | -from collections.abc import Callable, Generator |
| 22 | +from collections.abc import Generator |
23 | 23 | from contextlib import contextmanager |
24 | 24 |
|
25 | 25 | from . import _typings as t |
| 26 | +from ._faux_native import PriorityWaiter |
26 | 27 |
|
27 | 28 | __all__ = ["PrioritySemaphore", "priority_context"] |
28 | 29 |
|
|
31 | 32 | _priority: contextvars.ContextVar[int] = contextvars.ContextVar("_priority", default=0) |
32 | 33 |
|
33 | 34 |
|
34 | | -class PriorityWaiter(tuple[int, float, asyncio.Future[None]]): |
35 | | - __slots__ = () |
36 | | - |
37 | | - def __new__(cls, priority: int, ts: float, future: asyncio.Future[None]) -> t.Self: |
38 | | - return super().__new__(cls, (priority, ts, future)) |
39 | | - |
40 | | - @property |
41 | | - def priority(self) -> int: |
42 | | - return self[0] |
43 | | - |
44 | | - @property |
45 | | - def ts(self) -> float: |
46 | | - return self[1] |
47 | | - |
48 | | - @property |
49 | | - def future(self) -> asyncio.Future[None]: |
50 | | - return self[2] |
51 | | - |
52 | | - @property |
53 | | - def cancelled(self) -> Callable[[], bool]: |
54 | | - return self.future.cancelled |
55 | | - |
56 | | - @property |
57 | | - def done(self) -> Callable[[], bool]: |
58 | | - return self.future.done |
59 | | - |
60 | | - def __await__(self) -> Generator[t.Any, t.Any, None]: |
61 | | - return self.future.__await__() |
62 | | - |
63 | | - def __lt__(self, other: t.Any) -> bool: |
64 | | - if not isinstance(other, PriorityWaiter): |
65 | | - return NotImplemented |
66 | | - return self[:2] < other[:2] |
67 | | - |
68 | | - |
69 | 35 | @contextmanager |
70 | 36 | def priority_context(priority: int, /) -> Generator[None, None, None]: |
71 | 37 | """Set the priority for all PrioritySemaphore use in this context. |
|
0 commit comments