Reactor Realtime Engine
A small, model-agnostic package for serving stateful models that generate per-session output chunks. It defines the engine lifecycle, a reusable scheduling loop, and implementations for batched and per-session execution.
The package depends only on NumPy. Models may use PyTorch, CUDA, or another backend without adding those dependencies to the engine.
Contents
- Install
- Quick start
- Choose an engine
- ComposedEngine
- Contract
- Latency
- Develop and test
Install
pip install reactor-realtime-engine
Python 3.10 or newer is required.
Quick start
from realtime_engine import ComposedEngine, SessionInit
from realtime_engine.examples import CpuStageModel
engine = ComposedEngine(
CpuStageModel(max_sessions=2),
pipeline=False,
)
engine.attach(SessionInit("alice", {"seed": 1, "prompt": "a castle"}))
engine.attach(SessionInit("bob", {"seed": 2, "prompt": "a desert"}))
results = engine.step(
{
"alice": {"move": 1},
"bob": {"move": -1},
}
)
for result in results:
print(result.session_id, result.output.shape, result.stats)
engine.finalize([result.session_id for result in results])
engine.detach("alice")
engine.detach("bob")
CpuStageModel is a deterministic example. Production models implement
StageModel or the lower-level RealtimeInterface contract.
Choose an engine
ComposedEngineshares a fixed-row batch for efficiency. Model stages must keep per-row state isolated.ReplicaEnginegives each session an independent B=1 child. This simplifies state isolation but provides no cross-session batching benefit and may use more memory.- A custom
RealtimeInterfaceprovides full control for models that need their own scheduling or execution strategy.
ReplicaEngine pools successfully detached child engines for reuse. It is also
useful as a B=1 correctness reference while adding batched model support.
ComposedEngine
ComposedEngine drives a StageModel through HOT generation, optional
DEFERRED decoding, and isolated ENCODE work for changed heavy conditioning.
It owns admission, fixed-row batching, cohort membership, and completion events.
Pipelining uses lazily created CUDA streams when available and otherwise runs
the same schedule sequentially.
Contract
Every engine implements manifest, attach, step, finalize, and detach.
attach()raisesAdmissionErrorwhen capacity is unavailable.step(due)advances a batch of{session_id: conditioning}entries. It may return fewer results during pipeline warm-up or temporary row isolation; callers must not assume one result per due session.SetActive(session_id, False)removes an attached session from scheduled steps while preserving its model state and latest conditioning.finalize()performs optional post-chunk maintenance.detach()releases session state and is idempotent.
An engine may implement serve(inbox, outbox, *, stop) to own its scheduling
loop. Otherwise, run_engine() uses default_serve(), which serializes
lifecycle calls on one worker thread and paces toward the declared rate.
Inbox and Outbox are in-process queue adapters for lifecycle messages and
generated chunks. Reactor Runtime's RealtimePipeline connects these queues to
client state, media tracks, output pacing, and connection lifecycle.
Latency
input_latency_chunks: when boundary-aligned conditioning affects generation.pipeline_depth_chunks: how many scheduler ticks prime the output pipeline.
action-to-output chunks = input_latency_chunks + pipeline_depth_chunks - 1
End-to-end action-to-pixels latency also includes queueing, transport, output pacing, and rendering.
Develop and test
cd backend/realtime_engine
pip install -e ".[dev]"
ruff check src tests && black --check src tests && isort --check-only src tests
mypy src/realtime_engine
pytest -q
The test suite covers admission, deterministic row reuse, solo-versus-batched invariance, cross-row isolation, pipeline warm-up, heavy-conditioning changes, lifecycle ordering, failure recovery, replica pooling, and B=1 parity.
Metadata
Release files for reactor-realtime-engine 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| reactor_realtime_engine-0.3.0.tar.gz | 32.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| reactor_realtime_engine-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 61.5 kB
Release files / reactor_realtime_engine-0.3.0.tar.gz
| Download URL | reactor_realtime_engine-0.3.0.tar.gz |
|---|---|
| Size | 32.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3098ddbebfbc387f20648817923fed03c275c3ddee47b26004dcfd8070510508
|
|
BLAKE2b-256 checksum How to use checksums |
b9495c97c4e7664b035ee914bee42b9303d40285ef75ab612cd469128b244961
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Release files / reactor_realtime_engine-0.3.0-py3-none-any.whl
| Download URL | reactor_realtime_engine-0.3.0-py3-none-any.whl |
|---|---|
| Size | 28.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e05a8619075cd2f1beed0a3b8fd9328d4230e3cb380ae23cf6fdbd7aed7ff628
|
|
BLAKE2b-256 checksum How to use checksums |
7f49cc56bacd53b5404ea7bcba79b39cd045495cb0ccdd9fe67fad1a1465dab7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|