One subscriber's position in an Atomic.
Functions#
__init__#
__init__(atomic) -> None| Parameter | Type | Description |
|---|---|---|
atomic | Atomic[A] |
def __init__(self, atomic: Atomic[A]) -> None:
self._atomic = atomic
self._index = atomic._end()
self._unsubscribed = Falsesize#
size() -> intHow 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() -> boolReturns bool
def is_empty(self) -> bool:
return self.size() == 0poll#
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 Nonepoll_up_to#
poll_up_to(n) -> list[A]Up to n messages (none if n is 0 or less).
| Parameter | Type | Description |
|---|---|---|
n | int |
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() -> NoneStop 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)