There are two ways to use tickertape journals from Python. Both read the same on-disk format (format.md).
Bindings: pip install tickertape |
Decoder: python/tickertape_reader.py |
|
|---|---|---|
| Install | A wheel (built from Rust with PyO3); CPython 3.11+ on Linux and macOS | Copy one file; Python 3.9+, no dependencies |
| Import | import tickertape |
import tickertape_reader |
| Write | Yes: Writer, the single writer, with the journal lock and crash recovery |
No |
| Read | Live cursor: replay from any sequence, follow the writer, wait with a timeout | One pass over what is committed when it runs; doesn't follow or wait |
| Batches | append_batch, read_many, columnar read_batch for NumPy/Arrow/pandas |
Generator of named tuples |
| Durable-only reads | Reader(durable_only=True), waits for checkpoints |
durable_only=True, capped at the checkpoint seen at startup |
| Validation | The Rust reader's (shared with the CLI) | An independent implementation of the specification |
| Implementation | Compiled Rust | Pure Python (slower) |
Use the bindings to record events from Python, to follow a journal live, or to read large journals quickly. Use the
decoder where you cannot install a compiled package, for offline analysis of a copied journal, or as an independent
check of the format. The decoder's documentation is python/README.md. For bulk analysis
without Python in the loop, tickertape export --format parquet (see cli.md) is another option.
Status: like the C ABI, the bindings are not stable before tickertape 1.0. What they promise from 1.0 is in
compatibility.md.
The package's own guide, which is also its PyPI page, is bindings/python/README.md.
The design decisions are in ADR 0029. In brief:
- Visible versus durable is exactly as in the crash contract: visible when
appendreturns, durable when a latercheckpoint()returns, or once a laterrequest_checkpoint()has finished (checkpoint_progress(); ADR 0038: the checkpoint runs on a library thread while appends go on, and a failure is raised once by the nextrequest_checkpoint(),checkpoint_progress(),checkpoint()orclose(), thenDurabilityLostError). Closing a writer does not checkpoint;close()waits for a background checkpoint in flight and raises a failure as Rust'sWriter::closedoes.Writer(early_writeback=..., helper_cpus=...)set the Linux-only writer options of ADR 0038, andWriter(spare_segment=...)(from 1.1) the spare segment (ADR 0049;None, the default, is on on macOS and off on Linux, and nothing reports the value in effect).Reader(durable_only=True)returns only checkpointed records. A blocking durable-only reader is woken by checkpoints, not by appends (ADR 0053, from 1.1; beside a 1.0.0 writer it polls the marker everymax_sleep); when the writer's helper threads share its CPU, the first record after a checkpoint can arrive up to about 1.5 ms after it (a median of 15–145 µs on cloud VMs) and the last record of a large backlog up to about 2 ms later than with a reader that polls the marker, so keep them on other CPUs (Writer(helper_cpus=...),operations.md→ Pinned writers). After a failed checkpoint, records appended before it may not be durable even if a writer opened later in the same boot checkpoints successfully. - Copies. Payloads are
bytesor buffers of unsigned bytes (uint8 only). Every record returned to Python owns its payload asbytes. Batch columns arebytesobjects that NumPy and pyarrow wrap without another copy. There is no live zero-copy access; the mapped Rust reader is not exposed. - Batches cross the language boundary once.
append_batchconverts and checks all arguments first, then appends them as one batch (Writer::append_batch, ADR 0041: if at least one record is appended and wake-ups are enabled, blocked readers are woken once after the last record appended, plus once at each roll inside the batch); on a failure part-way it raisesBatchAppendErrorsaying how many records were appended and from which sequence number. To retry, slice the payloads and every per-record metadata sequence atfailed_index, so keep them in sequences rather than one-shot iterators.read_manyandread_batchwait for the first record (up to a timeout, default 0), then return what is already committed. - Iteration.
for record in readerdrains the records committed so far without waiting; eachiter(reader)is a new iterator that stays exhausted once it stops.reader.next(timeout=None)waits. - Threads and signals. Blocking calls release the GIL and wake at least every 50 ms to check for signals with the
reader's lock released, so Ctrl-C works during a wait and signal handlers may use the reader. In every thread, a read
that raises consumed nothing, and a read that returns a value consumed exactly the records in it. Python runs signal
handlers only in the main thread; a read there delivers its records only once its result is complete and pending
signal handlers have run, so if a handler raises (also while a large result is being built, or while the read waits
for another thread using the reader), the records are returned by the next read, not skipped. The boundary no binding
can remove is the read's final signal check: a signal that arrives after it is handled when the call returns, after
the records are delivered (and a staging buffer too large to keep is freed), and if its handler raises before Python
stores the result, the result is lost. A read in another thread is not interrupted by signals. Other threads that keep
taking the reader's lock (a stream of
seekcalls) can delay a delivery, which stays interruptible. Objects can be shared between threads; a read that does not wait returns nothing only when no committed record is left; a waiting read holds its reader for at most 50 ms at a time, andclose()from another thread ends it withValueError. - Fork. Handles belong to the process that opened them (its process id and a count of forks, so a reused process
id does not fool the check); in a forked child they raise
UsedAfterForkError. Closing them in the child (or letting them be collected) releases the child's copies of the descriptors and mappings without unlocking or changing anything the parent uses, so the journal can be reopened once the parent closes its writer, while the child still runs. A handle that another thread was using at the fork is never released in the child (its descriptors stay open until the child exits or callsexec). Open handles after forking, or use thespawnstart method ofmultiprocessing.
tickertape.JournalStatus.sample(path) takes the read-only sample that tickertape status prints (ADR
0032; meaning and accuracy of each value:
operations.md). It needs no writer and releases the GIL. Writer.metrics() returns what
only the writer knows, and Reader.metrics(status) a reader's cursor and lag behind a sample:
s = tickertape.JournalStatus.sample("/data/journal")
print(s.exposure.records, s.checkpoint and s.checkpoint.estimated_age_ns, s.storage.estimated_rolls_left)
lag = reader.metrics(s).lag # None, with reader.metrics(s).lag_unknown, if it can't be established
w = writer.metrics() # in the writer's process: no system calls- Results are named tuples (
JournalStatusis a frozen dataclass); unknown values areNone, never guesses, often with a reason code (age_unknown,lag_unknown: the codes of the CLI's JSON). Durations are integer nanoseconds. - Corruption is
s.head.end == "invalid", as.checkpoint.problem, ors.head.chain_broken.s.head.verifiedfalse means the segments between the checkpoint and the newest were not read: no corruption reported then does not mean that a writer would open the journal. Reader.metricsmeasures against the sample, so take it after the reads you want counted. Records the reader has read from the journal but not returned yet (buffered) count in the lag.- Cost:
JournalStatus.sample()lists the directory, reads the checkpoint marker, scans the newest segment from the checkpoint (so it grows with what was appended since the last checkpoint), reads two segment headers,writer.lockand the beacons, and callsstatvfs; the metrics calls make no system calls. The Python bindings call the core crate directly (ADR 0029), not the C ABI.
The latency histograms (the writer's, too) and record ages of the Rust crate (tickertape::latency, from 1.1, ADR
0047) are not available from Python yet; tickertape age (see cli.md)
reports ages from the command line.
bindings/python/examples/end_to_end.py writes market-style events in
batches with a checkpoint after each, follows the journal live from a second process, verifies it, replays the
durable records from a chosen sequence number in one batch, and summarizes them with pandas and DuckDB:
pip install tickertape pandas duckdb pyarrow
python bindings/python/examples/end_to_end.py /tmp/tt-python-demoThe core of the hand-off to analysis:
import duckdb
import tickertape as tt
with tt.Reader("/tmp/tt-python-demo", durable_only=True) as reader:
frame = reader.read_batch(1_000_000).to_pandas()
print(duckdb.sql("SELECT schema_id, count(*) FROM frame GROUP BY schema_id"))cd bindings/python
python -m venv .venv && . .venv/bin/activate
pip install maturin pytest
maturin develop --release --features test-hooks
pytestThe test-hooks feature adds the test-only methods that some tests need to order races and simulate a handle locked at
a fork; without it those tests are skipped. Release wheels are built without it.
The cross-language tests also need the Rust CLI and fixture helpers; without them they are skipped:
cargo build --release --locked -p tickertape-cli -p tickertape-examples -p tickertape-tests --bins
TICKERTAPE_BIN=../../target/release/tickertape TICKERTAPE_PRODUCER=../../target/release/demo-producer \
TICKERTAPE_DURABLE_HELPER=../../target/release/tt-durable-only pytestThe bindings crate is not a member of the Cargo workspace and has its own Cargo.lock; lint it with
cargo clippy --manifest-path bindings/python/Cargo.toml -- -D warnings.