Skip to main content

Messagebuffer

A buffer of pending messages.

Attributes​

AttributeTypeDescription
EmptyException = EmptyException class raised when attempting to take a message from an empty buffer.
maxsizeintThe maximum number of messages allowed in the buffer before eviction occurs.
datadequeThe underlying double-ended queue used to store and manage the sequence of pending messages.

Constructor​

Signature​

def Messagebuffer(
maxsize: int,
iterable: Iterable = None,
deque: Any = deque
) - > None

Parameters​

NameTypeDescription
maxsizeintThe maximum number of messages the buffer can hold before eviction occurs.
iterableIterable = NoneAn optional collection of initial messages to populate the buffer.
dequeAny = dequeThe deque implementation used for internal storage.

Methods​


put()​

@classmethod
def put(
item: Any
)

Adds a single message to the end of the buffer and triggers eviction if the buffer exceeds its maximum size.

Parameters​

NameTypeDescription
itemAnyThe message object to be stored in the buffer.

extend()​

@classmethod
def extend(
it: Iterable
)

Appends multiple messages from an iterable to the buffer and performs eviction if the resulting size exceeds the limit.

Parameters​

NameTypeDescription
itIterableA collection of message objects to be added to the buffer.

take()​

@classmethod
def take(
*default: Any
) - > Any

Removes and returns the oldest message from the buffer. Raises an Empty exception if the buffer is empty and no default value is provided.

Parameters​

NameTypeDescription
*defaultAnyAn optional value to return if the buffer is empty instead of raising an exception.

Returns​

TypeDescription
AnyThe oldest message in the buffer or the provided default value if the buffer is empty.