普贤教程 › 包参考
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_add | sched, name, interval_ms, cb |
sched_counts | sched |
sched_run | sched, total_ms |
其余 3 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
sched_remove | sched, name |
sched_tick | sched, now_ms |
sched_next_run | sched |
包内说明(源码头注释)
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 基准累计,避免漂移。 ============================================================