mmapq
Python bindings for mmapq — a persistent, indexed, replayable inter-process queue backed by memory-mapped files. No broker, no daemon; the queue is just files.
Why Python for a sub-microsecond queue?
Not for the hot path — for the read side. The pattern mmapq is built for
fast producers (C or Rust: market data, sequenced commands, sensor and
telemetry streams) on one side of a persistent, indexed log, and lets any
number of consumers read that log independently. Python is the natural
language for the consumer: replay, analysis, notebooks, dashboards,
projections into pandas — over the exact same queue files the fast
producers write, with no serialization bridge, no message broker, and no
copy out of shared memory beyond the one bytes() you ask for.
Because the queue is indexed and replayable, the Python side gets the capability the whole architecture exists for: replay is the product. Copy a queue directory from production to a laptop and re-read it from index zero — the same operation that drives recovery and replication is also how you debug, backtest, and audit. “What did command K do” is a single indexed read from a Python script. The event-sourcing guide in the mmapq repository describes the full pattern; the feature matrix gives exact coverage across C, Rust, and Python.
Quick start
Use a directory on a persistent filesystem. The examples create ./data;
/dev/shm is volatile memory and /tmp may be cleared on reboot. Both are
useful for disposable tests and benchmarks, not retained queue history.
Commit publishes messages to readers; it does not flush them to stable storage. mmapq relies on OS writeback and does not guarantee durability through power loss. Consumer positions are application state: save the last processed index and seek explicitly when resuming. Export files also need an appropriate storage and backup policy.
Single-message queue
from pathlib import Path
from mmapq import IndexedQueue, NextMove
Path("./data").mkdir(exist_ok=True) # the base directory must exist
def handler(index, appender_id, data):
print(f"[{index}] {data!r}")
return NextMove.ADVANCE
with IndexedQueue.create("./data", "my_queue") as queue:
with queue.create_appender() as appender:
index = appender.append(b"hello world")
with queue.create_poller() as poller:
poller.move_to_index(index)
poller.poll(handler)
Batch queue (atomic multi-message commit)
from pathlib import Path
from mmapq import IndexedBatchQueue
Path("./data").mkdir(exist_ok=True) # the base directory must exist
with IndexedBatchQueue.create("./data", "events") as queue:
with queue.create_appender() as appender:
with appender.start_batch() as batch:
batch.append(b"event 1")
batch.append(b"event 2")
batch.append(b"event 3")
index = batch.commit()
with queue.create_reader() as reader:
assert reader.read(index) == [b"event 1", b"event 2", b"event 3"]
Leaving the with block without commit() rolls the batch back; the
discarded batch reappears in recovery and can be committed by the lane
owner. A crash-interrupted open batch is reported once by
join_completed_open_batch(). An exception after a successful commit does
not undo published messages. Use one active batch per appender; the writer
cannot be reused after commit, rollback, or appender close.
Reading and replay (random access)
Reopen the batch queue written above, using the matching batch reader and export function:
from mmapq import IndexedBatchReader, export_batch_queue
with IndexedBatchReader.create("./data", "events") as reader:
first = reader.read_first()
last = reader.read_last()
assert last == [b"event 1", b"event 2", b"event 3"]
print(reader.last_index())
export_batch_queue("./data", "events", "./data/events.mmapq")
A read returns None only when the index is not yet written. An index below
the queue’s start index (archived) raises ArchivedError, a LookupError
carrying .index:
from mmapq import ArchivedError, IndexedBatchReader
with IndexedBatchReader.create("./data", "events") as reader:
assert reader.read(reader.last_index() + 1) is None # not yet written
try:
reader.read(0)
except ArchivedError as err: # only once start_index has passed 0
print("archived:", err.index)
For a single-message queue, use IndexedReader and export_queue instead:
from mmapq import IndexedReader, export_queue
with IndexedReader.create("./data", "my_queue") as reader:
first = reader.read_first() # bytes | None
last = reader.read_last()
idx = reader.last_index() # -1 when the queue is empty
msg = reader.read(idx) if idx >= 0 else None # read any committed index
print(idx)
export_queue("./data", "my_queue", "./data/my_queue.mmapq")
Recovery — uncommitted messages and batches
After a restart, messages appended but never committed (and batches staged
or discarded before a crash) are recoverable by the rejoining lane owner.
The iterators snapshot at call time; do not append while iterating. Commit
found offsets with commit() / commit_batch() — any subset, any order;
skipping is fine. Archiving may remove messages the walk has not yielded,
and discarded batches reappear in recovery — recovery code decides.
After a crash, recover through the same lane. A writer that owns a fixed
lane ID rejoins it with create_appender_with_id(id); create_appender()
takes a free lane, and its walk finds nothing of the crashed writer’s. A
writer killed without closing leaves its ID taken
(AppenderIdConflictError) until an operator who has established that the
owner is dead releases it with mmapq-ids --unset (the tool from the
mmapq repository; from its root:
make mmapq-ids, then
tools/mmapq-ids/build/release/mmapq-ids <dir> <queue> --unset <id> — the
tool is not installed on your PATH); doc/guide/05-crash-and-restart.md
there gives the procedure. A writer that closed cleanly released its ID.
After a crash the queue keeps every record’s framing, not its body: a
record being written when the crash happened keeps its full length, but its
data may be partly written, and the library cannot tell it from a complete
record. Validate each uncommitted record with your own check (a sequence
number or checksum in the payload) before committing it. A record that is
not committed reappears in every later walk until archiving passes it. The
queue only provides the walk: a rejoin never commits anything, and what to
commit, skip or append again is your code’s decision. On a batch lane,
join_completed_open_batch() reports — once — the batch the rejoin
completed to staged (not committed) after a crash; it reappears in later
walks unreported. The contract is in doc/guide/05-crash-and-restart.md
and an example of a strictly ordered writer’s restart at the end of
doc/guide/04-resequenced-commits.md, both in the mmapq repository.
from mmapq import IndexedQueue
def is_valid(data):
# The application's own integrity check — for example a checksum or a
# sequence number carried in the payload; the library has none.
return len(data) > 0 # replace with your check
with IndexedQueue.create("./data", "my_queue") as queue:
with queue.create_appender_with_id(0) as appender: # this writer's fixed lane
for offset, data in appender.uncommitted():
print(f"recovered {len(data)} bytes at {offset}")
# After a crash a record's data may be partly written, and an
# uncommitted record reappears in every later walk: commit only
# what your own check accepts.
if is_valid(data):
appender.commit(offset) # publish; skipping is also valid
# Batch lanes: appender.uncommitted_batches() yields (offset, messages);
# appender.commit_batch(offset) publishes; appender.join_completed_open_batch()
# reports a crash-interrupted batch the rejoin completed to staged.
# BatchWriter.complete_uncommitted() stages without publishing.
A note on validation and safety
The Python extension compiles the C library with -DNDEBUG, so it runs the
release build with internal asserts stripped. Input validation therefore
happens in the Python layer and is mandatory, not defensive decoration:
empty appends, out-of-range indices, and closed-handle use are checked
before the FFI call and raise ValueError / QueueClosedError. Corruption
detected on open or during a poll raises QueueCorruptError (distinct from
a transient timeout) rather than returning silently. Every wrapper object
supports explicit close(), is a context manager, and is finalized safely
at interpreter shutdown.
The raw cffi FFI/lib objects are deliberately not exported as
mmapq.ffi / mmapq.lib. Every C symbol reachable through them has no
argument checking — one bad call segfaults the interpreter. If you need
low-level access (e.g. iterator access), reach the raw objects only through
the documented escape hatches, and only when you know what you are doing:
from mmapq import unsafe_ffi, unsafe_lib
ffi = unsafe_ffi() # cffi FFI object
lib = unsafe_lib() # loaded C library
Error messages
Messages for C return codes come from the C library (mmapq_strerror), so
Python and C text cannot diverge. The Python mapping is defined in
_errors.py;
the C table in
doc/reference/error-codes.md
is informative. In particular: MMAPQ_ENOENT raises
QueueFileNotFoundError (also a FileNotFoundError), MMAPQ_ENOTREADY
raises QueueNotReadyError, MMAPQ_EIO raises the base MmapqError
(a permission, path or filesystem problem — fix the cause), and
MMAPQ_ECALLBACK is reported as the base MmapqError.
Archiving has two more classes. archive_up_to_index on a queue that is
not archivable raises NotArchivableError (a ValueError). When it cannot
open the queue (for example before the queue is created) it raises an
ArchiveOpenError subclass; each is also the class for its return code
and a ValueError (the call raised ValueError for these cases before
0.2.1): ArchiveOpenFailedError (-1, -10), ArchiveOpenOutOfMemoryError
(-9), ArchiveOpenCorruptError (-4), ArchiveOpenSchemaMismatchError
(-5), ArchiveOpenNotFoundError (-7), ArchiveOpenNotReadyError (-11).
Installation
pip install mmapq
On systems that mark the system Python as externally managed (PEP 668 —
for example Ubuntu 24.04), install into a virtual environment:
python3 -m venv .venv && . .venv/bin/activate.
From source
git clone --recurse-submodules https://github.com/algoteq-labs/mmapq-py
cd mmapq-py
pip install -e . # builds the cffi extension against the C sources
pip install mmapq installs a prebuilt wheel on Linux x86-64 and ARM64
with CPython 3.10–3.15 and glibc 2.28 or newer (for example Ubuntu 20.04,
Debian 10, RHEL 8 and later); no compiler is needed. Elsewhere — an older
glibc, musl (Alpine), another Python version — pip builds the source
package instead, and so does pip install -e .. A source build needs a C
compiler. pip uses the compiler your Python was built with; if the build
fails because that compiler is not installed — for example
error: [Errno 2] No such file or directory: 'clang' — name one that is:
CC=gcc pip install mmapq (or CC=gcc pip install -e .).
API
Exports
The authoritative export list is mmapq.__all__ in
mmapq-py/src/mmapq/__init__.py
and the Python README. Commonly used names are shown below; this is not
claimed to be the complete list.
from mmapq import (
NextMove, PollResult, BatchPollResult, BatchEntryStatus, EntryStatus,
ConflictBehavior, APPENDER_ID_AUTO,
IndexedQueue, IndexedAppender, IndexedPoller,
IndexedBatchQueue, IndexedBatchPoller, IndexedBatchAppender, BatchWriter,
IndexedReader, IndexedBatchReader,
Runtime, IndexedArchiver, IndexedBatchArchiver,
MmapqError, QueueClosedError, QueueCorruptError, QueueNotReadyError,
SchemaMismatchError, QueueExistsError, QueueFileNotFoundError,
AppenderIdConflictError, MmapOutOfMemoryError, ArchivedError,
NotArchivableError, ArchiveOpenError,
export_queue, export_batch_queue, import_queue, import_batch_stream,
import_batch_queue, import_flat_to_batch,
copy_queue, copy_batch_queue, copy_normal_to_batch, copy_batch_to_normal,
check_rc, unsafe_ffi, unsafe_lib,
)
Single-message handler callback
The handler passed to poller.poll() receives (index: int, appender_id: int, data: bytes):
def handler(index, appender_id, data):
print(f"[{index}] {data}")
return NextMove.ADVANCE
Batch handler callback
The handler passed to poller.poll() receives (batch_index: int, messages: list[bytes]):
def handler(batch_index, messages):
# messages is a list of payload bytes, one per message in the batch
for msg in messages:
print(f"[{batch_index}] {msg}")
return NextMove.ADVANCE
Queue creation
IndexedQueue.create(...) and IndexedBatchQueue.create(...) take the
following signatures; defaults match the C library’s
INDEXED_QUEUE_DEFAULT_* constants:
IndexedQueue.create(
directory, queue_name, write_runtime=None, read_runtime=None,
archivable=False, *, region_size=4096*1024, ring_size=4,
max_file_size=None, start_index=0, timeout_ns=10_000_000_000,
runtime=None,
)
IndexedBatchQueue.create(
directory, queue_name, write_runtime=None, read_runtime=None, *,
region_size=4096*1024, ring_size=4, max_file_size=None, start_index=0,
timeout_ns=10_000_000_000, archivable=False, runtime=None,
)
Geometry arguments are validated on every call, including for an existing
queue: an invalid region_size, ring_size, max_file_size, or
start_index raises. archivable, region_size, and max_file_size take
effect only when this handle creates the queue’s first appender; for an
existing queue the on-disk configuration is authoritative for them.
ring_size and timeout_ns apply to each handle.
For both queue types, create writes nothing: the queue’s files are written
when its first appender is created, with the settings of the queue handle it
came from. Create the queue and its first appender in one place with the
intended settings, including start_index.
Additional appender methods
IndexedAppender (single-message) also provides:
append_uncommitted(data)— writes a payload without publishing it. Readers cannot see it. After a crash, the rejoining owner of the same lane can walk and commit it; validate the body with the application’s own sequence number or checksum before committing. Returns a payload offset for later use withcommit().commit(payload_offset)— publishes a previously uncommitted payload. Returns the committed index.
IndexedQueue (single-message) also provides:
create_appender_with_id(appender_id)— creates an appender with a specific lane ID (0–255). RaisesValueErrorif the ID is out of range,AppenderIdConflictErrorif the ID is taken.
IndexedBatchQueue (batch) also provides:
create_appender_with_id(appender_id)— creates a batch appender with a specific lane ID (0–255). RaisesValueErrorif the ID is out of range.
IndexedBatchAppender (batch) also provides:
id()— returns this appender’s lane ID (0–255).
Sync vs async mapping
By default mapping happens synchronously on the hot path. For async background
region mapping, pass a Runtime:
from mmapq import Runtime, IndexedQueue
rt = Runtime.region_mapper()
q = IndexedQueue.create("./data", "rt_queue", runtime=rt)
# Runtime is kept alive by the queue — safe to drop the local reference
del rt
Archiving
An archivable queue (archivable=True) can advance its start index so old
entries become inaccessible and their backing files can be safely deleted.
The setting takes effect only if this handle creates the queue’s first
appender (see create above); an archiving process can check
arch.archivable() first. It raises a retryable FileNotFoundError,
QueueNotReadyError or MmapqError until the queue’s first appender has
written its files — retry on the same archiver:
from pathlib import Path
from mmapq import IndexedArchiver, IndexedQueue
Path("./data").mkdir(exist_ok=True) # the base directory must exist
q = IndexedQueue.create("./data", "arch_q", archivable=True,
region_size=32*1024, max_file_size=32*1024)
with q.create_appender() as appender: # the first appender writes the queue files
for i in range(1000):
appender.append(f"event {i}".encode().ljust(64, b"."))
arch = IndexedArchiver.create("./data", "arch_q")
arch.archive_up_to_index(1000) # archive entries < 1000
files = arch.archivable_files() # files no live entry can need
deleted = arch.reclaim_files(files) # safe even against a concurrent undo
archivable_files() lists the files fully covered by the archive boundary, so
the example sizes files at 32 KiB to show reclamation; with the default 1 GiB
files nothing is reclaimed until a whole file is covered. Copy the marked
files aside if you keep long-term history, then delete the local copies with
reclaim_files(files). reclaim_files() re-lists and unlinks under one
archive-lock hold and is the recommended safe deletion path. Hand deletion is
safe only if no undo runs between the listing and the last unlink.
The rest of the contract is in chapter 6 of the mmapq repository’s guide
(doc/guide/06-archiving-and-reclamation.md): a snapshot is identified by
the next index to apply; archive only after the snapshot is durable; on
start and on PollResult.ARCHIVED — which holds the cursor — read
start_index() as S, move_to_index(S) (retry from reading
start_index() if it returns False), then load snapshot S. Files are
reclaimed whole, so freed space comes in max_file_size steps: with the
default 1 GiB files a lane frees nothing until it has written more than
1 GiB. Size max_file_size to the retention you want. Chapter 6 also has
the snapshotter and consumer loop written out in Python.
Transfer (export/import/copy)
For a replica on the same host, copy (copy_queue, copy_batch_queue;
preserve=True keeps the source’s index space, so a reader of the copy
sees the same indices; appender IDs are copied unchanged unless you pass a
mapper). For a portable file to store or move, export and import.
The library provides 10 transfer functions across single-message and batch queues:
from mmapq import (
export_queue, export_batch_queue,
import_queue, import_batch_stream, import_batch_queue,
import_flat_to_batch,
copy_queue, copy_batch_queue,
copy_normal_to_batch, copy_batch_to_normal,
)
# Export a queue to a portable flat file (single-message)
n = export_queue("./data", "src", "./data/export.bin")
# Import flat file into a new queue (allow_append=True appends to existing)
n = import_queue("./data/export.bin", "./data", "restored")
n = import_queue("./data/export.bin", "./data", "restored", allow_append=True)
# Copy queue files directly (avoids intermediate export file)
n = copy_queue("./data", "src", "./data", "copy")
# Preserve the source index space (start_index); appender IDs are preserved
# independently unless you pass a mapper
n = export_queue("./data", "src", "./data/preserve.bin", preserve=True)
n = import_queue("./data/preserve.bin", "./data", "preserved", preserve=True)
# Batch queue transfers
n = export_batch_queue("./data", "batch_src", "./data/batch_export.bin")
n = import_batch_queue("./data/batch_export.bin", "./data", "batch_dst")
n = import_batch_stream("./data/batch_export.bin", "./data", "flat_stream")
# Cross-type conversions (flat to batch, batch to flat)
n = import_flat_to_batch("./data/export.bin", "./data", "batch_queue")
n = copy_normal_to_batch("./data", "flat_src", "./data", "batch_dst")
n = copy_batch_to_normal("./data", "batch_src", "./data", "flat_dst")
# All copy/import functions accept:
# allow_append=True — append to existing queue instead of refusing
# preserve=True — keep the source index space (start_index)
# mapper=callable — map source appender IDs to destination IDs
# on_conflict=<enum> — ConflictBehavior.AUTO or .ABORT
Examples
Batch echo server
rm -rf /tmp/request /tmp/response
python3 examples/batch_echo_server.py /tmp request response &
python3 examples/batch_latency_bench.py 100000 10000 64 --external
Internal benchmark (single process)
rm -rf /tmp/request /tmp/response
python3 examples/batch_latency_bench.py 100000 10000 64
Performance
The Python layer adds roughly 1–2 µs per poll() call through the cffi
callback trampoline (C → Python → C dispatch). Expected throughput on
isolated cores is 100,000–200,000 ops/sec. This is by design and not the
point: Python is the consumer and analysis language here, not the
low-latency producer. The C-core benchmark is in the
C guide.
Reach for Python
Reach for the Python bindings when you want:
- A Python consumer, projection, or analysis process reading a log that C or Rust producers write — the replay/notebook/dashboard side of an event-sourced pipeline.
- Cross-process compatibility with existing C or Rust deployments sharing a queue (Java interop is not supported in 0.2.0 — Java-written queues are refused at open; planned at schema 2).
- Replay, export, and offline forensics over recorded queues.
- Prototyping and testing with the real wire format.
For low-latency production on the hot path, produce from the C or Rust bindings; let Python read.
Compatibility
C, Rust, and Python share the same queue file format. mmapq’s file format
derives from Algoteq’s Java implementation (an evolved derivative of
tools4j/mmap; not compatible with upstream tools4j). In 0.2.0, C, Rust and
Python do not share queues with Java: 0.2.0 refuses Java-written queues at
open (schema_version 1), and the Java implementation does not yet follow the
0.2.0 protocol (for example, it does not maintain the archive line). This
applies to both queue types. Read Java-written queues with the Java library.
Java support is planned at schema 2, under the same protocol contract (see
doc/reference/wire-format.md).
Stability
Frozen within a schema version. 0.2.0 writes schema_version=2 and refuses
0.1.0 files (schema_version=1) at open. To move 0.1.0 data forward, export
with a 0.1.0 library and import into a fresh 0.2.0 queue — the export stream
format is unchanged. Each release states the schema versions it reads; a
file of any other version is refused at open. A version change is always a
clean refusal, never a silent misread. The feature set is frozen and the API
grows only for capability already present in the C core.
In Python, a schema mismatch at open raises SchemaMismatchError.
Testing
python3 -m unittest discover tests
Acknowledgements
mmapq-py wraps Algoteq’s mmapq, originally derived from the
tools4j/mmap Java library. The
event-sourcing architecture follows
tools4j/event-sourcing and
elara.
License
BSD 3-Clause — see LICENSE.
Platform
Linux only (x86-64 and ARM64, both tested in CI). Requires naturally
aligned lock-free atomics over MAP_SHARED mmap. To check your own host, run
the C test suite in a source checkout: bash c/scripts/test.sh.
Hardening compatibility changes
Transfer mapper exceptions now stop the transfer and re-raise the original exception after C cleanup, rather than silently assigning another appender ID. Transfers are not atomic: an already committed prefix may remain after failure. Package versions are unchanged pending release.
IndexedReader.read_view() returns a read-only, zero-copy memoryview that
keeps the reader alive. Reads, index queries, and close() raise
BufferError while any exported view or alias remains. Release every view
with release() or a memoryview context manager before advancing or closing
the reader:
with reader.read_view(0) as view: # when entry 0 exists
payload = bytes(view) # optional owned copy
reader.close() # succeeds after all views/aliases release
Releasing the original view does not release surviving slices, casts, or
buffer consumers. A failed close() leaves the reader open, including on
reader context manager exit. If the block is already propagating an
exception, the reader’s context manager suppresses only the cleanup
BufferError, preserving that original exception. The reader remains open
with its finalizer armed; release all views and close explicitly, or allow
garbage collection once all references (including retained tracebacks) are
gone. Normal context exit still raises BufferError for an outstanding
borrow.
For nested reader/view contexts, release buffer consumers before leaving the
view context. For example, a retained numpy.frombuffer(view) array can
prevent view.release() from succeeding. Python’s memoryview context exit
may then raise BufferError and mask a processing exception; the reader
cannot suppress that inner cleanup failure. Surviving view aliases also keep
the reader borrowed.
Ordinary read() continues to return owned bytes. Raw FFI access
and external modification/truncation of committed files remain caller
obligations.
Commit publishes data, but does not guarantee power-loss durability. See the C persistence contract for restart, checkpoint, filesystem, and archival limitations.