普贤教程 › 包参考
actor 包
Actor 消息模式封装
⚠️ 含并发(spawn/chan)—— 必须编译模式运行(px build);
| 项 | 值 |
|---|---|
| 包名 | actor |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | actor.px |
| 分类 | 并发与通用模式 |
| 文件数 / 行数 | 1 / 86 |
安装与引入
bash
pxpkg init # 首次 pxpkg add actor@0.2.0 pxpkg install
px
import actor所属分类教程:并发与通用模式
公开接口
| 函数 | 参数 |
|---|---|
ac_new | handlers, init |
ac_send | actor, name, body |
ac_call | actor, name, body |
ac_stop | actor |
其余 6 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
ac_loop | actor |
ac_status | actor |
ac_state | actor |
ac_set_state | actor, state |
ac_processed | actor |
ac_alive | actor |
包内说明(源码头注释)
text
============================================================ 普贤生态库:registry/actor —— Actor 消息模式封装 ------------------------------------------------------------ ⚠️ 含并发(spawn/chan)—— 必须编译模式运行(px build); 解释器 px run 是 Mini 子集,无并发原语(PX-DEF-006)。 var a = ac_new({"inc": fn(state, body): state["n"] = state["n"] + body; return state["n"], "get": fn(state, body): return state["n"]}, {"n": 0}) ac_call(a, "inc", 5) → Ok(5) # 发消息并等待应答 ac_send(a, "inc", 3) → Ok(true) # 只发不等(fire-and-forget) ac_stop(a) # 优雅停止(处理完已排队消息) 约定: - 处理函数签名 fn(state, body) -> result;可原地修改 state(dict 引用语义) - 邮箱 FIFO:同一 actor 的消息按发送序串行处理,天然免锁 - 未注册消息名 → ac_call 返回 Err;ac_send 静默成功(不阻塞) 停止:向邮箱投递 __stop 哨兵;已排队消息会先于哨兵处理完 ============================================================ actor 主循环:收消息 → 按名分派 → 可选回执