【发布时间】:2019-02-08 19:57:42
【问题描述】:
我对使用 crontab 的每日计划任务有疑问。 这是我的 celery.py
app.conf.beat_schedule = {
'run-cache-updater': {
'task': 'tasks.run_cache_updater',
'schedule': crontab(
minute=0,
hour='1-4'
),
}
}
下面是我的tasks.py 我在那里做的是,从数据库中获取所有记录。触发其他作业以更新我在 Redis 上的缓存。
@app.task
def run_cache_updater():
batch_size = 1000
cache_records = models.CacheRecords.objects.all()
def _chunk_list(all_records, size_of_batch):
for i in range(0, len(all_records), size_of_batch):
yield [item.id for item in all_records[i: i + batch_size]]
for items in _chunk_list(cache_records, batch_size):
update_cache.delay(items)
@app.task
def update_cache(ids_in_chunks):
for id in ids_in_chunks:
# Some calls are done here. Then sleep for 200 ms.
time.sleep(0.2)
我的任务运行良好。但是,它们开始在 1 到 4 之间运行,然后每 4 小时再次开始,例如 8-11、15-18.. 我在这里做错了什么,我该如何解决?
【问题讨论】:
标签: python django cron celery scheduled-tasks