Skip to main content

HFS v2 - HuggingFace Space Worker 分布式调度系统

基于 Redis 的分布式 Worker 调度系统,用于管理 HuggingFace Space 资源池。

特性

  • ✅ 分布式调度 - 多 Worker 并发,自动负载均衡
  • ✅ 状态管理 - 原子操作(Lua 脚本),保证一致性
  • ✅ 健康检查 - 自动检测崩溃、超时、孤儿资源
  • ✅ 账号管理 - 多账号池,自动选择、cooldown、评分
  • ✅ Space 轮换 - 自动创建、绑定、轮换、复用
  • ✅ Admin CLI - 命令行管理工具

快速开始

1. 安装依赖

pip install redis huggingface-hub click tabulate

2. 初始化系统

cd v2
python admin/init.py

3. 查看状态

python admin/cli.py --redis-url="redis://..." list-nodes
python admin/cli.py --redis-url="redis://..." list-accounts

4. 运行 Worker

python -m hfs --redis-url="redis://..." --space-id=my-space --project-id=demo --node-id=node-1

架构

┌─────────────┐
│   Redis     │  ← 状态存储
└──────┬──────┘
       │
   ┌───┴────┐
   │        │
┌──▼──┐  ┌─▼───┐
│Worker│  │Worker│  ← 独立进程
└──┬──┘  └─┬───┘
   │       │
┌──▼───────▼──┐
│  Scheduler  │  ← 调度器
└─────────────┘

核心模块

  • state.py - 状态机 + 原子操作(Lua 脚本)
  • health.py - 健康检查(崩溃检测、一致性验证)
  • policy.py - 策略配置(场景、命名)
  • worker.py - Worker 主循环(心跳、进程管理)
  • scheduler.py - 调度器(分配、轮换、创建)
  • account.py - 账号管理(选择、cooldown、评分)
  • hf.py - HuggingFace API 封装

测试

# 运行所有测试
pytest tests/ -v

# 运行特定模块
pytest tests/test_state.py -v
pytest tests/test_worker.py -v
pytest tests/test_scheduler.py -v

文档

配置

Redis

export HFS_REDIS_URL="redis://:password@host:port/db"

HuggingFace 账号

在 admin/init.py 中配置账号列表:

ACCOUNTS = [
    {'username': 'user1', 'token': 'hf_xxx'},
    {'username': 'user2', 'token': 'hf_yyy'}
]

开发

测试驱动开发

  1. 先写测试(tests/test_*.py)
  2. 再实现功能(hfs/*.py)
  3. 运行测试验证

代码结构

v2/
├── hfs/                # Worker 包
│   ├── state.py        # 状态机
│   ├── health.py       # 健康检查
│   ├── policy.py       # 策略配置
│   ├── worker.py       # Worker 主循环
│   ├── scheduler.py    # 调度器
│   ├── account.py      # 账号管理
│   └── hf.py           # HF API
├── admin/              # Admin 工具
│   ├── cli.py          # 命令行工具
│   └── init.py         # 快速初始化
├── tests/              # 测试
└── docs/               # 文档

许可

MIT License

Metadata

Release files for mp2-worker 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mp2-worker 0.1.5
File Size Uploaded
mp2_worker-0.1.5.tar.gz 35.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mp2-worker 0.1.5
File Interpreter ABI Platform
mp2_worker-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 68.6 kB

Release files / mp2_worker-0.1.5.tar.gz

Download URL mp2_worker-0.1.5.tar.gz
Size 35.0 kB
Tags Source
SHA-256 checksum
How to use checksums
45735ac77cc8390ba1673131cfb6e4957f148b17c4924e3cc1eedee4269570e7
BLAKE2b-256 checksum
How to use checksums
778e0df8006254e49c44d2cd5d41a96fa8db9c9198e0f4eee36716c07ac3358d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / mp2_worker-0.1.5-py3-none-any.whl

Download URL mp2_worker-0.1.5-py3-none-any.whl
Size 33.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1dcfd5d1809339af091a595bf087dd3196df096fea846ccfb3b05f3d55b7b315
BLAKE2b-256 checksum
How to use checksums
67484dda3a4e9c6a86096476fbe6ab9fe2faab20b4d939bc90d342139027d8ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page