python-rq 是一个类似celery 的python 任务调度框架基于了redis

部署

需要包含worker 以及具体的任务执行,或者调度任务尺触发, 同时注意因为依赖redis 应该先启动redis

  • 启动worker
rq worker --with-scheduler
  • 1.
  • 任务开发
    注意任务需要使用独立的模块
import requests
 
def count_words_at_url(url):
    resp = requests.get(url)
    print(resp.text)
    return len(resp.text.split())
 
 
def generate_report():
    print("generate_report")
    return "this is a demo {0} ".format("dalong")
 
 
def task1(x, y):
    result = x + y
    return result
 
def task2(previous_result):
    result = previous_result * 2
    return result
运行任务
包含依赖的
from redis import Redis
from rq import Queue
from rq.job import Job
 
 
from task import count_words_at_url,generate_report
q = Queue(connection=Redis())
 
 
report_job = q.enqueue(generate_report)
result = q.enqueue(count_words_at_url,"https://pagedebug.com/10",depends_on=report_job)
 
print(result)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

 

说明

python-rq 也是支持任务的调度的,相对来说rq 也是比较轻量的,对于项目使用还是听方便的,实际上可以和apscheduler 集成起来使用,米弥补彼此能力不足的一些问题(比如apscheduler 缺少依赖处理),python-rq 官方文档还是比较全的,很值得看看

参考资料

 https://python-rq.org/docs/
 https://github.com/rq/rq
 https://github.com/rq/rq-scheduler
 https://apscheduler.readthedocs.io/