Skip to content

EventQueue

🛈 DocStrings

A deque guarded by an explicit lock (no reliance on deque atomicity / the GIL).

Source code in src/datacook/app/eventqueue.py
class EventQueue:
    """A deque guarded by an explicit lock (no reliance on deque atomicity / the GIL)."""

    def __init__(self) -> None:
        self._lock = threading.Lock()
        self._items: collections.deque[Event | Posted] = collections.deque()
        self._scheduled = False

    def put(self, item: Event | Posted) -> bool:
        """Append; True if the consumer must be woken (no drain is scheduled yet)."""
        with self._lock:
            self._items.append(item)
            if self._scheduled:
                return False
            self._scheduled = True
            return True

    def take(self) -> list[Event | Posted]:
        """Everything queued so far; the next `put` schedules a new drain."""
        with self._lock:
            items = list(self._items)
            self._items.clear()
            self._scheduled = False
            return items

    def __len__(self) -> int:
        with self._lock:
            return len(self._items)

put

put(item: Event | Posted) -> bool

Append; True if the consumer must be woken (no drain is scheduled yet).

Source code in src/datacook/app/eventqueue.py
def put(self, item: Event | Posted) -> bool:
    """Append; True if the consumer must be woken (no drain is scheduled yet)."""
    with self._lock:
        self._items.append(item)
        if self._scheduled:
            return False
        self._scheduled = True
        return True

take

take() -> list[Event | Posted]

Everything queued so far; the next put schedules a new drain.

Source code in src/datacook/app/eventqueue.py
def take(self) -> list[Event | Posted]:
    """Everything queued so far; the next `put` schedules a new drain."""
    with self._lock:
        items = list(self._items)
        self._items.clear()
        self._scheduled = False
        return items

:fa-brands-github: Show source on GitHub