Skip to content

fly_ruler_std.runtime.loop ​

本页由 fr-docs-dev gen-api 从源码 docstring 生成,请勿手工编辑;重新生成执行 just api。

仿真循环子包:任务、上下文与同步/异步执行器。

公开成员 ​

名称类型定义位置
SimulationContext类本页
LoopTask类本页
SyncLoop类本页
AsyncRealtimeLoop类本页
TaskMode类本页
OverrunPolicy类本页
LoopStats类本页
make_discrete_model_task函数本页
run_async_realtime函数本页
run_sync函数本页

定义 ​

SimulationContext ​

python
class SimulationContext(dt: float, time: float = 0.0, step: int = 0, wall_time: float = time_module.monotonic(), state: dict[str, object] = dict(), data: dict[str, object] = dict(), running: bool = True, stop_reason: str | None = None)

仿真循环任务间共享的可变上下文。

dt ​

python
dt: float

固定仿真步长,单位秒,必须为正。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:16

time ​

python
time: float = 0.0

当前仿真时间,单位秒。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:19

step ​

python
step: int = 0

已完成的仿真步数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:22

wall_time ​

python
wall_time: float = field(default_factory=time_module.monotonic)

最近一次推进时的墙上时钟时间,单位秒,取自 time.monotonic。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:25

state ​

python
state: dict[str, object] = field(default_factory=dict)

任务间共享的模型状态,按用户约定的键存取。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:28

data ​

python
data: dict[str, object] = field(default_factory=dict)

任务间共享的临时消息与诊断数据。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:31

running ​

python
running: bool = True

循环是否继续运行;request_stop 会将其置为 False。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:34

stop_reason ​

python
stop_reason: str | None = None

请求停止时记录的原因;未请求停止为 None。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:37

request_stop ​

python
def request_stop(reason: str | None = None) -> None

请求停止循环。

参数

名称类型默认值说明
reasonstr | NoneNone停止原因,写入 stop_reason;None 表示不记录原因。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:46

advance ​

python
def advance() -> None

推进一个步长:步数加一、仿真时间增加 dt 并刷新 wall_time。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:55

__init__ ​

python
def __init__(dt: float, time: float = 0.0, step: int = 0, wall_time: float = time_module.monotonic(), state: dict[str, object] = dict(), data: dict[str, object] = dict(), running: bool = True, stop_reason: str | None = None) -> None

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/context.py:12

LoopTask ​

python
class LoopTask(name: str, fn: Callable[[SimulationContext], object], rate_hz: float, stage: str = 'dynamics', order: int = 0, wait_for: Sequence[str] = tuple(), mode: TaskMode = TaskMode.BLOCKING, overrun: OverrunPolicy = OverrunPolicy.BLOCK, enabled: bool = True)

由运行时循环调度的用户任务。

name ​

python
name: str

任务唯一名称,同一循环内不可重复。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:111

fn ​

python
fn: Callable[[SimulationContext], object]

任务回调,接收 SimulationContext,可为同步或异步函数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:114

rate_hz ​

python
rate_hz: float

任务执行速率,单位赫兹,必须为正。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:117

stage ​

python
stage: str = 'dynamics'

任务所属阶段,用于阶段顺序排序,默认 dynamics。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:120

order ​

python
order: int = 0

同阶段内的排序键,默认 0。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:123

wait_for ​

python
wait_for: Sequence[str] = field(default_factory=tuple)

需要先完成的任务名序列,默认空。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:126

mode ​

python
mode: TaskMode = TaskMode.BLOCKING

执行模式,默认 TaskMode.BLOCKING。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:129

overrun ​

python
overrun: OverrunPolicy = OverrunPolicy.BLOCK

任务溢出策略,默认 OverrunPolicy.BLOCK。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:132

enabled ​

python
enabled: bool = True

是否参与调度,默认 True。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:135

__init__ ​

python
def __init__(name: str, fn: Callable[[SimulationContext], object], rate_hz: float, stage: str = 'dynamics', order: int = 0, wait_for: Sequence[str] = tuple(), mode: TaskMode = TaskMode.BLOCKING, overrun: OverrunPolicy = OverrunPolicy.BLOCK, enabled: bool = True) -> None

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:107

SyncLoop ​

python
class SyncLoop(ctx: SimulationContext, tasks: Sequence[LoopTask], *, stage_order: Sequence[str] | None = None)

确定性定步长仿真循环。

__init__ ​

python
def __init__(ctx: SimulationContext, tasks: Sequence[LoopTask], *, stage_order: Sequence[str] | None = None) -> None

按阶段与顺序整理任务、校验依赖并建立统计表。

参数

名称类型默认值说明
ctxSimulationContext—循环共享的仿真上下文。
tasksSequence[LoopTask]—待调度的任务序列。
stage_orderSequence[str] | NoneNone阶段顺序;None 使用 DEFAULT_STAGE_ORDER。

异常

类型说明
ValueError任务名重复,或 wait_for 引用了未知任务。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:82

ctx ​

python
ctx = ctx

循环共享的仿真上下文。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:101

tasks ​

python
tasks = ordered

按阶段、顺序与名称排序后的任务列表。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:103

stats ​

python
stats = {task.name: LoopStats() for task in ordered}

任务名到运行统计 LoopStats 的映射。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:105

run ​

python
def run(*, duration: float | None = None, max_steps: int | None = None, stop_when: StopWhen | None = None, timeout_wall_s: float | None = None) -> dict[str, LoopStats]

同步推进仿真直到满足任一停止条件。

每个步长内按 tasks 顺序执行到期任务,随后调用 ctx.advance() 前进。

参数

名称类型默认值说明
durationfloat | NoneNone仿真时长上限,单位秒;None 表示不限。
max_stepsint | NoneNone仿真步数上限;None 表示不限。
stop_whenStopWhen | NoneNone接收上下文、返回是否停止的回调;None 表示不检查。
timeout_wall_sfloat | NoneNone墙上时钟运行上限,单位秒;超时后以 timeout_wall_s 为原因请求停止;None 表示不限。

返回

类型说明
dict[str, LoopStats]任务名到 LoopStats 的映射。

异常

类型说明
RuntimeError同步依赖在同一步内尚未完成。
TypeError任务为异步函数,或返回可等待对象。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:108

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:79

AsyncRealtimeLoop ​

python
class AsyncRealtimeLoop(ctx: SimulationContext, tasks: Sequence[LoopTask], *, stage_order: Sequence[str] | None = None)

基于 asyncio 的尽力而为软实时定步长循环。

__init__ ​

python
def __init__(ctx: SimulationContext, tasks: Sequence[LoopTask], *, stage_order: Sequence[str] | None = None) -> None

按阶段与顺序整理任务并建立各任务的运行时状态。

参数

名称类型默认值说明
ctxSimulationContext—循环共享的仿真上下文。
tasksSequence[LoopTask]—待调度的任务序列。
stage_orderSequence[str] | NoneNone阶段顺序;None 使用 DEFAULT_STAGE_ORDER。

异常

类型说明
ValueError任务名重复,或 wait_for 引用了未知任务。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:183

ctx ​

python
ctx = ctx

循环共享的仿真上下文。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:202

tasks ​

python
tasks = ordered

按阶段、顺序与名称排序后的任务列表。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:204

stats ​

python
stats: dict[str, LoopStats]

任务名到运行统计 LoopStats 的映射。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:215

run ​

python
def run(*, duration: float | None = None, max_steps: int | None = None, stop_when: StopWhen | None = None) -> dict[str, LoopStats]

按墙上时钟节拍异步推进仿真直到满足任一停止条件。

每步统计调度滞后与错过截止时间次数,按任务模式与溢出策略调度到期任务,并尽力对齐 ctx.dt 的实时节拍;退出时取消尚未完成的任务。

参数

名称类型默认值说明
durationfloat | NoneNone仿真时长上限,单位秒;None 表示不限。
max_stepsint | NoneNone仿真步数上限;None 表示不限。
stop_whenStopWhen | NoneNone接收上下文、返回是否停止的回调;None 表示不检查。

返回

类型说明
dict[str, LoopStats]任务名到 LoopStats 的映射。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:219

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:180

TaskMode ​

python
class TaskMode(str, Enum)

循环任务的执行模式。

BLOCKING ​

python
BLOCKING = 'blocking'

阻塞执行:循环等待任务完成后再推进。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:19

BACKGROUND ​

python
BACKGROUND = 'background'

后台执行:任务交给后台调度,按溢出策略决定等待、跳过或并发。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:22

DETACHED ​

python
DETACHED = 'detached'

分离执行:同样交给后台调度路径处理。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:25

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:16

OverrunPolicy ​

python
class OverrunPolicy(str, Enum)

任务上一次运行未完成时的处理策略。

BLOCK ​

python
BLOCK = 'block'

阻塞等待上一次运行完成后再启动。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:32

SKIP ​

python
SKIP = 'skip'

跳过本次调度并计入跳过次数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:35

CANCEL_PREVIOUS ​

python
CANCEL_PREVIOUS = 'cancel_previous'

取消上一次仍在运行的任务再启动。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:38

QUEUE_LATEST ​

python
QUEUE_LATEST = 'queue_latest'

只保留最新一次待执行请求,合并中间请求。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:41

ALLOW_OVERLAP ​

python
ALLOW_OVERLAP = 'allow_overlap'

允许上一次运行继续并并发启动新实例。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:44

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:29

LoopStats ​

python
class LoopStats(runs: int = 0, skipped: int = 0, cancelled: int = 0, queued_latest: int = 0, overlaps: int = 0, missed_deadlines: int = 0, last_runtime_s: float = 0.0, max_runtime_s: float = 0.0, max_lag_s: float = 0.0, last_started_time: float | None = None, last_finished_time: float | None = None)

单个循环任务的运行统计。

runs ​

python
runs: int = 0

已完成运行的次数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:63

skipped ​

python
skipped: int = 0

因 skip 策略被跳过的调度次数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:66

cancelled ​

python
cancelled: int = 0

因 cancel_previous 策略被取消的运行次数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:69

queued_latest ​

python
queued_latest: int = 0

因 queue_latest 策略被合并为最新一次的次数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:72

overlaps ​

python
overlaps: int = 0

因 allow_overlap 策略产生的并发运行次数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:75

missed_deadlines ​

python
missed_deadlines: int = 0

调度滞后超过 dt 的累计次数。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:78

last_runtime_s ​

python
last_runtime_s: float = 0.0

最近一次运行耗时,单位秒。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:81

max_runtime_s ​

python
max_runtime_s: float = 0.0

单次运行的最大耗时,单位秒。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:84

max_lag_s ​

python
max_lag_s: float = 0.0

观察到的最大的调度滞后,单位秒。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:87

last_started_time ​

python
last_started_time: float | None = None

最近一次运行的墙上时钟开始时间,单位秒;未运行为 None。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:90

last_finished_time ​

python
last_finished_time: float | None = None

最近一次运行的墙上时钟结束时间,单位秒;未运行为 None。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:93

__init__ ​

python
def __init__(runs: int = 0, skipped: int = 0, cancelled: int = 0, queued_latest: int = 0, overlaps: int = 0, missed_deadlines: int = 0, last_runtime_s: float = 0.0, max_runtime_s: float = 0.0, max_lag_s: float = 0.0, last_started_time: float | None = None, last_finished_time: float | None = None) -> None

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/task.py:59

make_discrete_model_task ​

python
def make_discrete_model_task(model: _DiscreteModel[ValueT], *, name: str = 'model', rate_hz: float, x_key: str = 'x', xd_key: str = 'xd', u_key: str = 'u', p_key: str = 'p', y_key: str = 'y', effects_key: str = 'effects', stage: str = 'dynamics', order: int = 0, wait_for: tuple[str, ...] = ()) -> LoopTask

构造每次调用推进一次离散模型的循环任务。

回调从 ctx.state 按键取出 x、xd、u、p,调用 model.discrete_fn,再把返回的 x、xd、y、effects 写回 ctx.state。

参数

名称类型默认值说明
model_DiscreteModel[ValueT]—提供 discrete_fn 的已编译模型。
namestr'model'任务名称,默认 model。
rate_hzfloat—任务执行速率,单位赫兹。
x_keystr'x'ctx.state 中连续状态的键,默认 x。
xd_keystr'xd'ctx.state 中离散状态的键,默认 xd。
u_keystr'u'ctx.state 中输入的键,默认 u。
p_keystr'p'ctx.state 中参数的键,默认 p。
y_keystr'y'ctx.state 中输出的键,默认 y。
effects_keystr'effects'ctx.state 中效果量的键,默认 effects。
stagestr'dynamics'任务所属阶段,默认 dynamics。
orderint0同阶段内的排序键,默认 0。
wait_fortuple[str, ...]()需要先完成的任务名序列,默认空元组。

返回

类型说明
LoopTask可用于 SyncLoop 或 AsyncRealtimeLoop 的 LoopTask。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/helpers.py:21

run_async_realtime ​

python
def run_async_realtime(ctx: SimulationContext, tasks: Sequence[LoopTask], *, duration: float | None = None, max_steps: int | None = None, stop_when: StopWhen | None = None, stage_order: Sequence[str] | None = None) -> dict[str, LoopStats]

用 AsyncRealtimeLoop 一次性运行软实时的异步仿真。

参数

名称类型默认值说明
ctxSimulationContext—循环共享的仿真上下文。
tasksSequence[LoopTask]—待调度的任务序列。
durationfloat | NoneNone仿真时长上限,单位秒;None 表示不限。
max_stepsint | NoneNone仿真步数上限;None 表示不限。
stop_whenStopWhen | NoneNone接收上下文、返回是否停止的回调;None 表示不检查。
stage_orderSequence[str] | NoneNone阶段顺序;None 使用 DEFAULT_STAGE_ORDER。

返回

类型说明
dict[str, LoopStats]任务名到 LoopStats 的映射。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:420

run_sync ​

python
def run_sync(ctx: SimulationContext, tasks: Sequence[LoopTask], *, duration: float | None = None, max_steps: int | None = None, stop_when: StopWhen | None = None, timeout_wall_s: float | None = None, stage_order: Sequence[str] | None = None) -> dict[str, LoopStats]

用 SyncLoop 一次性运行同步仿真。

参数

名称类型默认值说明
ctxSimulationContext—循环共享的仿真上下文。
tasksSequence[LoopTask]—待调度的任务序列。
durationfloat | NoneNone仿真时长上限,单位秒;None 表示不限。
max_stepsint | NoneNone仿真步数上限;None 表示不限。
stop_whenStopWhen | NoneNone接收上下文、返回是否停止的回调;None 表示不检查。
timeout_wall_sfloat | NoneNone墙上时钟运行上限,单位秒;None 表示不限。
stage_orderSequence[str] | NoneNone阶段顺序;None 使用 DEFAULT_STAGE_ORDER。

返回

类型说明
dict[str, LoopStats]任务名到 LoopStats 的映射。

实现位置:packages/fly_ruler/python/fly_ruler_std/runtime/loop/runner.py:388

签名类型说明 ​

以下别名用于解释参数与返回值,不是本包的公开导出对象。

内部别名类型定义
StopWhentype StopWhen = Callable[[SimulationContext], bool]