jug 简单使用

jug 提供了cli 命令可以方便的进行任务执行以及监控,以下是关于多实例运行的一个示例

任务开发

就是一个计算平方的

  • tasks.py
from jug import TaskGenerator
import time

@TaskGenerator
def slow_task(i):
    time.sleep(2)  # 模拟耗时任务
    return i * i

# 定义多个任务
results = [slow_task(i) for i in range(503)]

  • 获取结果
import jug
import tasks
jug.init('tasks.py', 'redis://localhost:6379')

results = jug.task.value(tasks.results)

print(results)

运行&监控

  • Procfile

通过Procfile模式运行,执行jug execute --jugdir redis://localhost:6379 tasks.py

redis-server: redis-server
task1:  jug execute --jugdir redis://localhost:6379 tasks.py
task2:  jug execute --jugdir redis://localhost:6379 tasks.py
task3:  jug execute --jugdir redis://localhost:6379 tasks.py
task4:  jug execute --jugdir redis://localhost:6379 tasks.py
  • 运行
honcho start
  • 监控
jug status  --jugdir redis://localhost:6379   tasks.py
  • 效果

可以看到有4个任务在运行

redis 执行信息

说明

jug 对于多节点运行有一个比较有意思的设计,应该是任务的倍数,否则任务会有执行不完整的问题,当然内部的还需要进一步的学习

参考资料

https://github.com/luispedro/jug

https://jug.readthedocs.io/en/latest/

posted on 2025-03-20 08:00  荣锋亮  阅读(33)  评论(0)    收藏  举报

导航