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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file moat_lib_ring-0.2.3.tar.gz.
File metadata
- Download URL: moat_lib_ring-0.2.3.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed7a7acbb02f1a2817a8dcbabfd8f6e9d096d5d0e26e5cbfae3941d73383e1f9
|
|
| MD5 |
af06b26bfaa6eff9dbcd6adbeaff4a11
|
|
| BLAKE2b-256 |
35bc363f117bee6baef583a442f7a3d11523aaeca94fd1362b1177a68b4df467
|
File details
Details for the file moat_lib_ring-0.2.3-py3-none-any.whl.
File metadata
- Download URL: moat_lib_ring-0.2.3-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3bcf7c4f4f44c117fbf586b95544d0817059605139ecff9f297fc9ef35c90111
|
|
| MD5 |
b47efa959f08411f3c832fc9f2af9a02
|
|
| BLAKE2b-256 |
b4678e2e9526b1d100467fcdbffe3c179ff4167df03ce9b3a0a8388173589ee0
|