Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 1,494 Bytes
abf6a59 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | """Process-local, non-preemptive priority admission for engine work."""
import asyncio
from collections import deque
from contextvars import ContextVar
ANONYMOUS_WORK = ContextVar("anonymous_work", default=False)
class PrioritySemaphore(asyncio.Semaphore):
"""FIFO within each class; normal work always precedes waiting anonymous work."""
def __init__(self, value=1):
super().__init__(value)
self._normal = deque()
self._anonymous = deque()
async def acquire(self):
queue = self._anonymous if ANONYMOUS_WORK.get() else self._normal
future = asyncio.get_running_loop().create_future()
queue.append(future)
self._dispatch()
try:
await future
return True
except BaseException:
# A slot may already have been reserved when the waiter is cancelled.
if future.done() and not future.cancelled():
self._value += 1
raise
finally:
if future in queue:
queue.remove(future)
self._dispatch()
def _dispatch(self):
while self._value > 0 and (self._normal or self._anonymous):
queue = self._normal if self._normal else self._anonymous
future = queue.popleft()
if future.cancelled():
continue
self._value -= 1
future.set_result(True)
def release(self):
self._value += 1
self._dispatch()
|