相关:
Celery-详解
Celery+Redis实现异步任务(1)
Celery+Redis实现异步任务(2)
Celery+Redis实现异步任务(3)
定时任务
创建文件夹apps
创建/apps/celery_conf.py
from kombu import Queue
BROKER_URL = 'redis://localhost:6379/1'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/2'
CELERY_IMPORTS = (
'apps.beat_task'
)
# 队列
CELERY_QUEUES = (
Queue('baet_task', routing_key='beat_task')
)
# 设置定时任务
CELERYBEAT_SCHEDULE = {
'my_beat':{
'task':'apps.beat_task.beat_test'
# 'schedule':timedelta(seconds=5), # 每隔5秒钟
# 'schedule':crontab(minute='49', hour='11') # 定点执行
'schedule':crontab(minute='*/1'),
'args':(1, 2),
'kwargs':{"name":"xxxx"},
'options':{
'queue':'beat_test'
'routing_key':'beat_task'
}
}
}
创建 /apps/__init__.py
from celery import Celery
app = Celery('test_task')
app.config_from_object('apps.celery_conf')
创建 /apps/beat_task.py
import time
from apps import app
@app.task()
def beat_test(x, y, name):
print name
return "hello celery beat"
启动命令:
celery -B -A apps worker -l INFO -Q baet_task
定时任务参数说明:
- 可以是任何被apply_async()支持的参数
- task:执行的任务名字。
- schedule:执行的频率
- args:位置参数(list或者tuple)
- kwargs:键值参数(dict)
- options:执行选项(dict)
- relative:如果 relative是True,时间表(时钟时间)安排,意味着频率近似到最近的秒,分钟,小时或者天,这取决于时间差中的时间间隔
- 默认relative是false,频率不是近似,会相对于celery的启动时间
- crontable 参数:
- crontab()实例化的时候没有设置任何参数就是表示每一分钟。
- minute:分钟,范围0-59
- hour:小时,范围0-23
- day_of_week :星期几,范围0-6,以星期天为开始,也可以用'sun'表示星期天
- day_of_month:每月第几号,范围1-31
- month_of_year:月份,范围1-12
注意:
默认值都是*
,表示任意时刻
举例
- crontab():每分钟执行一次
- crontab(minute=15):每小时15分钟执行一次
- crontab(minute=0, hour=0):每天0点0分执行一次
- crontab(minute='59', hour='11'):每天11点59分执行一次
- crontab(minute='0, 30'):每小时0分和30分执行一次,逗号表示
or
- crontab(hour='9-12, 20'):每天9点到12点和20点中的每分钟执行一次
- crontab(minute=0, hour=0):每天凌晨执行
- crontab(minute=0, hour='*/3'):每3小时执行一次
- crontab(minute=0, hour='0,3,6,9,12,15,18,21'):每3小时执行一次
- crontab(minute='*/15'):每15分钟执行一次
- crontab(day_of_week='sunday'):星期天每分钟执行一次
- crontab(minute='', hour='', day_of_week='sun'):星期天每分钟执行一次
- crontab(minute=’*/10’,hour=’3,17,22’, day_of_week=’thu,fri’) 每10分钟执行,仅限于周六日3-4 am, 5-6 pm, and 10-11 pm
- crontab(minute=0, hour=’/2,/3’) 偶数小时或者能被3整除的小时数执行
- crontab(minute=0, hour=’*/5’) 被5整除的小时数,如3pm
- crontab(minute=0, hour=’*/3,8-17’) 8am-5pm能被3整除的
- crontab(0, 0, day_of_month=’2’) 每月第2天
- crontab(0, 0,day_of_month=’2-30/3’) 每偶数天
- crontab(0, 0,day_of_month=’1-7,15-21’) 每月1和3周
- crontab(0, 0, day_of_month=’11’,month_of_year=’5’) 每年5月11日
- crontab(0, 0,month_of_year=’*/3’) 每个季度第1月
网友评论