API Reference

Shared Circular Buffer

class shared_cbuff.buffer.SharedCircularBuffer(name: str, *, create: bool = False, item_size: int = 1, length: int = 2)

An implementation of a circular buffer built on top of multiprocessing.shared_memory.SharedMemory to allow a fast method to send and receive data between multiple python instances.

Parameters

name (str) – The name of the memory block, used when linking SharedCircularBuffer instances together.

Keyword Arguments
  • create (bool) – Whether the class should create a new SharedMemory instance or link itself to one that already exists. Defaults to False

  • item_size (int) – The number of bytes each item in the buffer should take up. Defaults to 1.

  • length (int) – The length of the buffer. Defaults to 2.

Warning

All instances that connect to the same shared memory must have the name, item_size and length parameters passed in if they differ from default otherwise reading from the shared memory will not work correctly.

cleanup()None

Closes the connection to the SharedMemory block and unlinks it if this class was the writer to the buffer.

This method is automatically called before the program exits.

Returns

None

item_size

The maximum size of each item stored in bytes.

length

The length of the buffer.

name

The name of the shared memory block this instance is attached to.

popitem() → Optional[int]

Pops an item from the buffer.

Returns

Item removed from the buffer, or None if there is nothing to read.

Return type

Optional[ int ]

Raises

ReadOperationsForbidden – The buffer cannot be read from by this instance.

popmany(n: int) → Sequence[int]

Pops up to a maximum of n items from the buffer.

Returns

Items removed from the buffer.

Return type

Sequence[ int ]

Raises

ReadOperationsForbidden – The buffer cannot be read from by this instance.

push(item: int)None

Pushes an item to the buffer.

Parameters

item (int) – The item to put into the buffer.

Returns

None

Raises

WriteOperationsForbidden – The buffer cannot be written to by this instance.

property usage

The number of elements currently in the buffer. This can be used to figure out how full or empty the buffer is at any time.

Returns

Number of items in the buffer

Return type

int

Errors

exception shared_cbuff.errors.CBuffException

Bases: Exception

Base exception for all exceptions raised by the library.

exception shared_cbuff.errors.WriteOperationsForbidden

Bases: shared_cbuff.errors.CBuffException

Exception raised when a write operation is attempted but is not permitted.

exception shared_cbuff.errors.ReadOperationsForbidden

Bases: shared_cbuff.errors.CBuffException

Exception raised when a read operation is attempted but is not permitted.

exception shared_cbuff.errors.BufferAlreadyCreated

Bases: shared_cbuff.errors.CBuffException

Exception raised when attempting to create multiple buffers with the same name.