Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeshardmind committed Apr 16, 2024
1 parent 801a2bd commit ce312a7
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions async_utils/priority_sem.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

_priority: contextvars.ContextVar[int] = contextvars.ContextVar("_priority", default=0)


class PriorityWaiter(NamedTuple):
priority: int
ts: float
Expand Down Expand Up @@ -76,6 +77,7 @@ class PrioritySemaphore:
async with sem:
...
"""

_loop: asyncio.AbstractEventLoop | None = None

def _get_loop(self):
Expand All @@ -99,14 +101,13 @@ def __repr__(self):
res = super().__repr__()
extra = "locked" if self.locked() else f"unlocked, value:{self._value}"
if self._waiters:
extra = f'{extra}, waiters:{len(self._waiters)}'
extra = f"{extra}, waiters:{len(self._waiters)}"
return f"<{res[1:-1]} [{extra}]>"

def locked(self) -> bool:
# Must do a comparison based on priority then FIFO
# in the case of existing waiters, not guaranteed to be immediately available
return self._value == 0 or (
any(not w.cancelled() for w in (self._waiters or ())))
return self._value == 0 or (any(not w.cancelled() for w in (self._waiters or ())))

async def __aenter__(self):
prio = _priority.get()
Expand Down Expand Up @@ -165,4 +166,4 @@ def _maybe_wake(self):

def release(self):
self._value += 1
self._maybe_wake()
self._maybe_wake()

0 comments on commit ce312a7

Please sign in to comment.