GitHub

BackingSubscription

API reference for BackingSubscription.

One subscriber's position in an Atomic.

Functions

__init__

__init__(atomic) -> None
ParameterTypeDescription
atomicAtomic[A]
def __init__(self, atomic: Atomic[A]) -> None:
    self._atomic = atomic
    self._index = atomic._end()
    self._unsubscribed = False

size

size() -> int

How many messages are there to read.

Returns int

def size(self) -> int:
    """How many messages are there to read."""
    if self._unsubscribed:
        return 0
    atomic = self._atomic
    return atomic._end() - max(self._index, atomic._start)

is_empty

is_empty() -> bool

Returns bool

def is_empty(self) -> bool:
    return self.size() == 0

poll

poll() -> Option[A]

The next message, if there is one.

Returns Option[A]

def poll(self) -> Option[A]:
    """The next message, if there is one."""
    items = self.poll_up_to(1)
    return Some(items[0]) if items else None

poll_up_to

poll_up_to(n) -> list[A]

Up to n messages (none if n is 0 or less).

ParameterTypeDescription
nint

Returns list[A]

def poll_up_to(self, n: int) -> list[A]:
    """Up to ``n`` messages (none if ``n`` is 0 or less)."""
    count = min(n, self.size())
    if count <= 0:
        return []
    atomic = self._atomic
    start = max(self._index, atomic._start)
    self._index = start + count
    return [atomic._read(i) for i in range(start, start + count)]

poll_all

poll_all() -> list[A]

Returns list[A]

def poll_all(self) -> list[A]:
    return self.poll_up_to(self.size())

unsubscribe

unsubscribe() -> None

Stop reading; messages only this subscriber owed are dropped.

def unsubscribe(self) -> None:
    """Stop reading; messages only this subscriber owed are dropped."""
    if not self._unsubscribed:
        self._unsubscribed = True
        self._atomic._leave(self._index)