RQScheduler是一个小型的Python包,用来给RQ添加作业调度功能。
安装:
pip install rq-scheduler示例代码:
from redis import Redisfrom rq_scheduler import Schedulerfrom datetime import datetimescheduler = 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 laterscheduler.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)
评论