Messagebuffer
A buffer of pending messages.
Attributes
| Attribute | Type | Description |
|---|---|---|
| Empty | Exception = Empty | Exception class raised when attempting to take a message from an empty buffer. |
| maxsize | int | The maximum number of messages allowed in the buffer before eviction occurs. |
| data | deque | The 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
| Name | Type | Description |
|---|---|---|
| maxsize | int | The maximum number of messages the buffer can hold before eviction occurs. |
| iterable | Iterable = None | An optional collection of initial messages to populate the buffer. |
| deque | Any = deque | The 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
| Name | Type | Description |
|---|---|---|
| item | Any | The 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
| Name | Type | Description |
|---|---|---|
| it | Iterable | A 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
| Name | Type | Description |
|---|---|---|
| *default | Any | An optional value to return if the buffer is empty instead of raising an exception. |
Returns
| Type | Description |
|---|---|
Any | The oldest message in the buffer or the provided default value if the buffer is empty. |