Skip to main content

轻量级 step 幂等/断点续跑 checkpoint。默认文件存储,SQLite 标准库开箱即用,全 API 对齐 TS mooncat-checkpoint。

Project description

mooncat-checkpoint (Python)

mooncat-checkpoint 的 Python SDK。 与 TypeScript 版协议、JSON 格式、file/sqlite 数据完全互通零运行时依赖(sqlite3/json/secrets/hashlib 全是标准库),SQLite 开箱即用(无需 better-sqlite3 编译)。

安装

# uv(推荐)
uv pip install mooncat-checkpoint

# 或 pip(从本地)
pip install -e .

要求 Python ≥ 3.10。

快速开始

from mooncat_checkpoint import CheckpointClient, LeaseLostError

cp = CheckpointClient("file://./.mooncat/checkpoints")

c = cp.claim("vision", image_hash, owner_run_id="run-1")
if c.kind == "done":
    print("已处理,复用:", c.checkpoint.result)
elif c.kind == "claimed":
    try:
        result = expensive_call()
        cp.done("vision", image_hash, lease_token=c.lease_token, result={"ok": True})
    except LeaseLostError:
        pass  # lease 被重抢,放弃
    except Exception as e:
        cp.fail("vision", image_hash, str(e), lease_token=c.lease_token)
        raise

与 TypeScript 互通

Python SDK 与 TS mooncat-checkpoint 共享:

  • 协议:状态机、fencing token、错误类型一一对应
  • file 后端:JSON 格式完全一致(camelCase 键),TS 写的文件 Python 直接读,反之亦然
  • sqlite 后端:表结构完全一致,TS 写的 db Python 直接读
  • key hashsafe_key_segment 用相同 sha256 算法,同 key 在 TS/Python 产生相同 storage_key
# Python 读 TS 写的文件
cp = CheckpointClient("file:///path/to/ts-checkpoints")
r = cp.claim("vision", "img-001")  # 读到 TS done 的记录
// TS 读 Python 写的文件
const cp = new CheckpointClient({ url: "file:///path/to/py-checkpoints" });
const r = await cp.claim("vision", "img-001");

API(与 TS 对齐)

cp.claim(ns, key, owner_run_id=None, lease_ms=None)       ClaimResultclaimed  lease_token
cp.touch(ns, key, lease_token, extend_ms=None)             Checkpoint | None
cp.done(ns, key, lease_token, result=None, result_ref=None)  Checkpoint
cp.fail(ns, key, error, lease_token)                        Checkpoint
cp.get(ns, key)                                             Checkpoint | None
cp.reset(ns, key)                                           bool
cp.list(ns, status=None)                                    list[Checkpoint]
cp.gc(older_than=0)                                         int

后端

后端 URL 并发安全 说明
file(默认) file://./.mooncat/checkpoints 单进程 JSON 文件,透明可读
sqlite sqlite://./checkpoints.db 跨进程 标准库,开箱即用(Python 相对 TS 的优势)
memory memory:// 单进程 测试用

测试

uv run --with pytest pytest

含跨语言互通测试(需 TS npm run build 后运行 tests/test_cross_language.py)。

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mooncat_checkpoint-0.1.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mooncat_checkpoint-0.1.0-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file mooncat_checkpoint-0.1.0.tar.gz.

File metadata

  • Download URL: mooncat_checkpoint-0.1.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mooncat_checkpoint-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f918f9573566a0bbfe406835cf8f47641289e18a0e6dcb131985bb7e075a82d5
MD5 f8bb122642274dc425531f050b0f57dd
BLAKE2b-256 9e093523d961e255641485b6e7a5f4acb0c83204eb73d17a46f2e51a4478f149

See more details on using hashes here.

File details

Details for the file mooncat_checkpoint-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mooncat_checkpoint-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mooncat_checkpoint-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e98fccf1fbb6fb993ea1cdadcc9494357fcf150d6f2695a5f4f3de4a4267687
MD5 f6cdf4fc7754368e2fecc6b759e2a6d6
BLAKE2b-256 c2e495809d1e8bcf561956d0df9485cb02cba2a227a70846a658765d0fa6be05

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page