Skip to main content

A character-based ring buffer

Project description

moat-lib-ring

% start synopsis % start main

A simple opinionated character-based ring buffer.

This buffer, well, buffers. It's async compatible and can handle writes overruns either by delaying the writer or by only keeping the newest data.

% end synopsis

Async operation

moat.lib.ring.aio provides an async version.

Async writes always wait until all bytes have been delivered to the buffer.

% end main

Usage

Usage is very simple:

from moat.lib.ring import RingBuffer

# Create buffer
ring = RingBuffer(200)

# write to it
nbytes = ring.write(b'Hello', drop=True)
assert nbytes == 5

# read from it
buf = bytearray(2)
assert ring.readinto(buf) == 2
assert buf == 'He'

Overflow handling

If you write with drop=True, which is the default, writing to the buffer will always succeed and return the length of the bytestring. On overflow, the last bufsize-1 bytes will be preserved. The first byte you read will be a null byte, to signal that data was lost.

drop=False means that writing will stall instead of destroying data: write will return a smaller length than requested. The caller is responsible for waiting until there is free space.

Locking

This code doesn't know if it's called from a thread or not. If required, please add your own.

Threaded operation

TODO. Requires a lock and thread-based events (or conditions).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moat_lib_ring-0.2.4.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moat_lib_ring-0.2.4-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file moat_lib_ring-0.2.4.tar.gz.

File metadata

  • Download URL: moat_lib_ring-0.2.4.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for moat_lib_ring-0.2.4.tar.gz
Algorithm Hash digest
SHA256 3d5b688f8d2ad839ebe392b4e01fdb4b92b2ea69f4a51af35fa467fbd58ef118
MD5 b64807316da8c0189383a4da5b665acf
BLAKE2b-256 ffec2e3fff4fdedc761bdc0cc761a2a4a79dd33cb4976cd545d25fd309f9eb08

See more details on using hashes here.

File details

Details for the file moat_lib_ring-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: moat_lib_ring-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for moat_lib_ring-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d90cfb27a7434a4d9dfcc6eef71f9a48d4201cd580901f9c57a8be72c6d74b8c
MD5 d8e633ba8326dc141454a2532aa0aafc
BLAKE2b-256 4e691c8d009fa848245d372729236d7fc2e81e49208ad2ea8b39bfbb594f964c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page