普贤教程 › 包参考
retry 包
带退避的重试
| 项 | 值 |
|---|---|
| 包名 | retry |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | retry.px |
| 分类 | 并发与通用模式 |
| 文件数 / 行数 | 1 / 129 |
安装与引入
bash
pxpkg init # 首次 pxpkg add retry@0.2.0 pxpkg install
px
import retry所属分类教程:并发与通用模式
公开接口
| 函数 | 参数 |
|---|---|
retry_call | f, arg, opts=null |
其余 9 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
retry_opts | opts |
retry_pow2 | n |
retry_delay_ms | cfg, attempt |
retry_err_msg | r |
retry_delay_jittered | cfg, attempt |
retry_is_retryable | err_msg |
retry_stats | result |
retry_backoff_series | max_attempts, base_ms=100, strategy="exp" |
retry_max_delay | max_attempts, base_ms=100, strategy="exp" |
包内说明(源码头注释)
text
============================================================ 普贤生态库:registry/retry —— 带退避的重试 ------------------------------------------------------------ 纯函数库(无 IO、无状态),编译(px build)与解释(px run)双模式一致。 retry_call(fn, arg, opts?) → Ok(result) | Err fn(arg) 须返回 Result;成功(Ok)即返回,失败(Err)按策略重试 opts: times 最大尝试次数(默认 3) delay_ms 基础退避毫秒(默认 0) backoff "fixed"(每次 delay)| "linear"(delay×attempt)| "exp"(delay×2^attempt,默认 "exp") 任务"第几次尝试"通过参数长度约定:调用方在 fn 内可感知失败原因。 全部失败 → Err("retry: 尝试 N 次后失败: <last>") ============================================================