RQ Scheduler 是一个小型的 Python 包,用来给 RQ 添加作业调度功能。
安装:
pip install rq-scheduler
示例代码:
from redis import Redis from rq_scheduler import Scheduler from datetime import datetime scheduler = Scheduler(connection=Redis()) # Get a scheduler for the "default" queue # Puts a job into the scheduler. The API is similar to RQ except that it # takes a datetime object as first argument. So for example to schedule a # job to run on Jan 1st 2020 we do: scheduler.enqueue_at(datetime(2020, 1, 1), func) # Here's another example scheduling a job to run at a specific date and time (in UTC), # complete with args and kwargs. scheduler.enqueue_at(datetime(2020, 1, 1, 3, 4), func, foo, bar=baz)
第二种方法:
from datetime import timedelta # Schedule a job to run 10 minutes, 1 hour and 1 day later scheduler.enqueue_in(timedelta(minutes=10), count_retweets, tweet_id) scheduler.enqueue_in(timedelta(hours=1), count_retweets, tweet_id) scheduler.enqueue_in(timedelta(days=1), count_retweets, tweet_id)