Native Python Fallback¶
PyEventEngine includes a pure Python implementation (event_engine.native) that mirrors the Cython API exactly. This fallback is automatically used when Cython extensions fail to compile or are unavailable.
Overview¶
The native fallback provides identical functionality with reduced performance compared to the Cython version.
Architecture¶
The native fallback consists of three modules:
event_engine.native.topic- Topic parsing and matching (pure Python)event_engine.native.event- Event hooks and message payloads (pure Python)event_engine.native.engine- Event engine and queue management (threading+deque)
All classes use __slots__ for memory efficiency and expose the same API as the Cython version.
Key Differences¶
No C extensions - All code is pure Python using stdlib only
No custom allocators - Uses Python’s memory management
No ByteMap - Uses built-in
dictfor topic routingThreading-based - Uses
threading.Lockandthreading.Condition``owner`` property - Always returns
True(all Python objects own their data)
Performance¶
Expected performance characteristics:
Topic parsing: ~10-20x slower than Cython
Event dispatch: ~5-10x slower than Cython
Throughput: ~50-200k msg/s (vs ~500k-1M+ msg/s for Cython)
Latency: ~0.01-0.1ms additional overhead per message
Using the Fallback¶
Import Directly¶
Check Active Backend¶
API Compatibility¶
The fallback provides 100% API compatibility with the Cython version. .. code-block:: python
# Works identically on both backends topic = Topic(‘Market.Data.{symbol}’) formatted = topic.format(symbol=’AAPL’) engine = EventEngine(capacity=8192) engine.start() engine.put(topic, ‘data’) engine.stop()
Performance Tips¶
Pre-intern topics - Parse once, reuse many times
Minimize handler work - Keep handlers fast
Use exact topics - Faster than pattern matching
Batch operations - Publish multiple items together
Testing¶
Run native implementation tests: .. code-block:: bash
python demo/test_native_topic.py python demo/test_native_event.py python demo/test_native_engine.py python demo/native_performance_test.py
See Also¶
Python API Reference (CAPI) - Cython Python API (identical interface)
Installation - Building Cython extensions
Examples - Usage examples (work with both backends)