35 lines
860 B
Python
35 lines
860 B
Python
"""Bounded notifications from the registry writer to local operator streams."""
|
|
|
|
import asyncio
|
|
import threading
|
|
|
|
|
|
class FleetEvents:
|
|
def __init__(self):
|
|
self.lock = threading.Lock()
|
|
self.listeners = set()
|
|
|
|
def subscribe(self):
|
|
loop = asyncio.get_running_loop()
|
|
queue = asyncio.Queue(maxsize=1)
|
|
|
|
def put_latest():
|
|
if not queue.full():
|
|
queue.put_nowait(None)
|
|
|
|
listener = (loop, put_latest)
|
|
with self.lock:
|
|
self.listeners.add(listener)
|
|
|
|
def close():
|
|
with self.lock:
|
|
self.listeners.discard(listener)
|
|
|
|
return queue, close
|
|
|
|
def notify(self):
|
|
with self.lock:
|
|
for loop, callback in self.listeners:
|
|
if not loop.is_closed():
|
|
loop.call_soon_threadsafe(callback)
|