No project description provided
Project description
VoidRail
VoidRail 的名称来自于古老的修仙界,是虚空传送阵的意思。
VoidRail 基于 Celery 构建轻量级分布式任务处理框架,专为 CPU 密集型计算设计。它提供简单易用的接口,让您可以快速构建和部署分布式计算服务。
安装
使用 pip 安装:
pip install voidrail
核心组件
VoidRail 采用两组件架构:
- Worker:服务实现模块,继承
CeleryWorker基类,定义处理逻辑 - Client:客户端模块,使用
CeleryClient类发送任务请求
基本使用
简单例子
import sys
import os
import time
from voidrail import app, start_worker, get_config
@app.task(name='hello.say_hello')
def say_hello(name):
"""简单的问候任务"""
return f"Hello, {name}! Current time: {time.ctime()}"
@app.task(name='hello.say_hello_delay', bind=True)
def say_hello_delay(self, name, delay=3):
"""带延迟的问候任务,演示任务状态更新"""
self.update_state(state='PROGRESS', meta={'progress': 0, 'message': '开始处理'})
# 模拟处理过程
for i in range(10):
time.sleep(delay / 10)
self.update_state(state='PROGRESS', meta={
'progress': (i + 1) * 10,
'message': f'处理中 {(i + 1) * 10}%'
})
return f"Hello after {delay} seconds, {name}! Time: {time.ctime()}"
def main():
"""命令行入口点"""
# 显示服务信息
config = get_config()
print(f"Broker URL: {config['broker_url']}")
print(f"后端 URL: {config['result_backend']}")
# 启动Worker
start_worker()
if __name__ == "__main__":
main()
使用客户端
import sys
import os
import time
import json
from voidrail.config import get_config
from voidrail.client import CeleryClient
# 创建客户端
client = CeleryClient(service_name="hello")
# 同步调用
result = client.call(
task_name="say_hello",
args=["World"]
)
print(result["result"]) # 输出: Hello, World!
# 异步调用
async_result = client.call(
task_name="say_hello_delay",
args=["Async World"],
kwargs={"delay": 2},
wait_result=False
)
task_id = async_result["task_id"]
# 检查任务状态
status = client.get_task_status(task_id)
水平扩展能力
VoidRail的一个主要优势是支持简单而强大的水平扩展。当您启动多个相同服务的Worker实例时:
- 自动负载均衡:所有实例会自动协作处理队列中的任务
- 无需额外配置:不需要任何特殊设置,只需启动更多相同的服务实例
- 容错和高可用:如果某个实例崩溃,其他实例会继续处理任务
例如,您可以在多台服务器上启动相同的服务:
graph TB
Client[客户端]
Queue[(Redis消息队列)]
subgraph "服务器A"
Worker1[Worker实例1]
end
subgraph "服务器B"
Worker2[Worker实例2]
Worker3[Worker实例3]
end
subgraph "服务器C"
Worker4[Worker实例4]
end
Client -->|发送任务| Queue -->|分发任务| Worker1 & Worker2 & Worker3 & Worker4
运行多个Worker实例
要充分利用多核CPU,可以启动多个Worker实例:
# 启动Worker进程
# 通过环境变量控制并发度
CELERY_CONCURRENCY=4 python hello_service.py
您可以在不同的服务器上多次启动相同的服务实例:
# 在服务器A上
python hello_service.py
# 在服务器B上
python hello_service.py
# 在服务器C上
python hello_service.py
每个实例都会自动加入相同的worker池,共同处理任务队列。Celery会为每个worker分配一个唯一ID, 确保任务只会被处理一次。这种设计使VoidRail非常适合需要动态扩展的场景 - 随着负载增加, 只需启动更多的worker实例即可线性提高处理能力。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file voidrail-0.3.2.tar.gz.
File metadata
- Download URL: voidrail-0.3.2.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.0 CPython/3.11.7 Darwin/24.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb38905f498e439c05fe0bbaf30b939089e1df8e3aac8ee99c529d0b1017cf0f
|
|
| MD5 |
c1da2b21c6cc1c352a03c28c9b86ada2
|
|
| BLAKE2b-256 |
bfc52d6e37b68d7bae46067396ad7a1f3d5f8192631cbe2416012000458e211c
|
File details
Details for the file voidrail-0.3.2-py3-none-any.whl.
File metadata
- Download URL: voidrail-0.3.2-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.0 CPython/3.11.7 Darwin/24.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34e2d6754abef552b9e7ac3127633529cba71dd447b902b2d7af61effe0ac289
|
|
| MD5 |
36fb4356270f6ee4b36a74aee8e8d9fc
|
|
| BLAKE2b-256 |
95ebb18fe1b6b98c47368c844ab77d38ba50ee7d45410a0aa3721b4323d94e37
|