Skip to content

Commit c1edd0d

Browse files
committed
On second thought, the other option of removing immutability and writing a slotted class...
1 parent c757e13 commit c1edd0d

File tree

3 files changed

+25
-125
lines changed

3 files changed

+25
-125
lines changed

src/async_utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
__author__ = "Michael Hall"
1010
__license__ = "Apache-2.0"
1111
__copyright__ = "Copyright 2020-Present Michael Hall"
12-
__version__ = "2025.01.12"
12+
__version__ = "2025.01.14"
1313

1414
import os
1515
import sys

src/async_utils/_faux_native.py

Lines changed: 0 additions & 123 deletions
This file was deleted.

src/async_utils/priority_sem.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import threading
2222
from collections.abc import Generator
2323
from contextlib import contextmanager
24+
from operator import attrgetter
2425

2526
from . import _typings as t
26-
from ._faux_native import PriorityWaiter
2727

2828
__all__ = ["PrioritySemaphore", "priority_context"]
2929

@@ -32,6 +32,29 @@
3232
_priority: contextvars.ContextVar[int] = contextvars.ContextVar("_priority", default=0)
3333

3434

35+
class PriorityWaiter:
36+
__slots__ = ("future", "ord")
37+
38+
def __init__(self, priority: int, ts: float, future: asyncio.Future[None], /) -> None:
39+
self.ord = (priority, ts)
40+
self.future = future
41+
42+
cancelled = property(attrgetter("future.cancelled"))
43+
done = property(attrgetter("future.done"))
44+
__await__: t.Any = property(attrgetter("future.__await__"))
45+
46+
def __lt__(self: t.Self, other: object) -> bool:
47+
if not isinstance(other, PriorityWaiter):
48+
return NotImplemented
49+
return self.ord < other.ord
50+
51+
__final__ = True
52+
53+
def __init_subclass__(cls) -> t.Never:
54+
msg = "Don't subclass this"
55+
raise RuntimeError(msg)
56+
57+
3558
@contextmanager
3659
def priority_context(priority: int, /) -> Generator[None, None, None]:
3760
"""Set the priority for all PrioritySemaphore use in this context.

0 commit comments

Comments
 (0)