px普贤教程PuXian
普贤教程 › 包参考

sched 包

轻量任务调度器(时间片轮询)

对照 Python schedule / Go robfig/cron 心智的 interval 子集:

项值
包名sched
版本0.2.0
可用版本0.1.0 / 0.2.0
入口sched.px
分类并发与通用模式
文件数 / 行数1 / 103

安装与引入

bash
pxpkg init          # 首次
pxpkg add sched@0.2.0
pxpkg install
px
import sched
所属分类教程:并发与通用模式

公开接口

函数参数
sched_new
sched_addsched, name, interval_ms, cb
sched_countssched
sched_runsched, total_ms
其余 3 个 def(内部与辅助,可直接调用但不属稳定接口)
函数参数
sched_removesched, name
sched_ticksched, now_ms
sched_next_runsched

包内说明(源码头注释)

text
============================================================
普贤生态库:registry/sched —— 轻量任务调度器(时间片轮询)
------------------------------------------------------------
对照 Python schedule / Go robfig/cron 心智的 interval 子集:
sched_new()                            → dict        新建调度器
sched_add(sched, name, interval_ms, fn) → dict       注册周期任务(返回新/原调度器)
sched_run(sched, total_ms)             → dict       运行 total_ms;返回带 counts 的调度器
sched_counts(sched)                    → dict       {任务名: 触发次数}
设计说明:
- 不用 spawn/set_interval(解释轨不支持并发,PX-DEF-006):主线程时间片轮询,
保证双轨行为一致且可测(本轮原意是 native cron 包装,但独立调度循环更贴合纯 .px 库)。
- fn() 无参;副作用由调用方闭包完成(闭包按引用捕获,M160)。
- 触发次数 ≈ floor(total/interval) ±1;相邻周期用 next 基准累计,避免漂移。
============================================================