rfx.session module provides a Session class that connects a policy to a robot with lifecycle management, rate control, jitter tracking, error handling, and clean shutdown.
Session
Hardened inference-robot connection with lifecycle management. Connects a policy callable to a robot with rate control, jitter tracking, error handling, and clean shutdown. Thread-based, no async forced on users.Constructor
Any object satisfying the Robot protocol (observe/act/reset)
Callable that maps an observation dict to an action tensor
Target control loop frequency in Hz
Seconds to sleep after reset before starting the loop
Properties
Current number of control loop iterations (thread-safe)
Whether the control loop is currently running
Loop timing statistics (thread-safe)
Methods
start()
Reset robot and spawn the daemon control thread.stop()
Signal stop and join the control thread. Idempotent - safe to call multiple times.run(duration=None)
Blocking run.None means infinite until Ctrl+C or stop().
Maximum run time in seconds.
None for infinite.check_health()
Raise if the control thread encountered an error.Context Manager
Session can be used as a context manager for automatic cleanup:SessionStats
Loop timing summary for jitter and overrun analysis. Immutable dataclass.Fields
Total number of completed iterations
Number of timing overruns (iterations that exceeded target period)
Target loop period in seconds (1 / rate_hz)
Average actual loop period in seconds
Median jitter (50th percentile) in seconds
95th percentile jitter in seconds
99th percentile jitter in seconds
Maximum jitter observed in seconds
Methods
to_dict()
Convert stats to a dictionary.Dictionary with all stats fields
run()
One-liner function to connect a policy to a robot and run inference.Any object satisfying the Robot protocol
Callable mapping observation dict to action tensor
Target control loop frequency in Hz
Run time in seconds.
None for infinite (Ctrl+C to stop)Seconds to sleep after reset before starting the loop
Timing and jitter statistics from the run
Example Usage
Basic Session
Manual Control
One-Liner Deployment
Continuous Monitoring
Performance Notes
- The control loop uses a hybrid sleep strategy: coarse sleep + busy spin for the last ~1.2ms to minimize jitter
- Jitter is calculated as
|actual_period - target_period| - Overruns occur when loop execution time exceeds the target period
- Statistics are tracked with a maximum of 10,000 samples (configurable via
_MAX_TIMING_SAMPLES) - The control loop is thread-based and runs as a daemon thread
- All property accesses are thread-safe
