Scheduler
一种常见的需求是每隔一段时间执行一个任务.配置如下:
config.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import absolute_import
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/5'
BROKER_URL = 'redis://127.0.0.1:6379/6'
CELERY_TIMEZONE = 'Asia/Shanghai'
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
'add-every-30-seconds': {
'task': 'proj.tasks.add',
'schedule': timedelta(seconds=30),
'args': (16, 16)
},
}
注意配置文件需要指定时区.这段代码表示每隔30秒执行add函数.
一旦使用了scheduler,启动celery需要加上-B参数
celery -A proj worker -B -l info
crontab
计划任务当然也可以用crontab实现,celery也有crontab模式,修改config.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import absolute_import
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/5'
BROKER_URL = 'redis://127.0.0.1:6379/6'
CELERY_TIMEZONE = 'Asia/Shanghai'
from celery.schedules import crontab
CELERYBEAT_SCHEDULE = {
# Executes every Monday morning at 7:30 A.M
'add-every-monday-morning': {
'task': 'tasks.add',
'schedule': crontab(hour=7, minute=30, day_of_week=1),
'args': (16, 16),
},
}
总而言之,scheduler的切分度更细,可以精确到秒.crontab模式就不用说了.当然celery还有更高级的用法,比如多个机器使用,启动多个worker并发处理等.
网友评论