我们如何使用celery执行一些周期性任务呢?
celery beat是一个调度器,它可以周期内指定某个worker来执行某个任务。如果我们想周期执行某个任务需要增加beat_schedule配置信息.
celeryconfig.py配置信息如下:
ROKER_URL = 'redis://:332572@127.0.0.1:6379/1'
CELERY_RESULT_BACKEND = 'redis://:332572@127.0.0.1:6379/2'
CELERYBEAT_SCHEDULE = {
'every-5-minute':
{
'task': 'proj.tasks.period_task',
'schedule': 5.0,
'args': (16, 16),
}
}
CELERY_ROUTES = ({
'proj.tasks.send_email': {'queue': 'queue1'},
'proj.tasks.upload_file': {'queue': 'queue1'},
'proj.tasks.image_process': {'queue': 'queue2'},
},
# route_for_task,
)
CELERYBEAT_SCHEDULE配置每隔5秒执行一次period_task任务, 'every-5-minute'为任务名称,可自定义。task指定周期执行的任务函数,schedule指定多久执行一次task任务, args为传递给任务函数的参数。
tasks.py模块内容如下:
from proj.celery import app as celery_app
import time
# 创建任务函数
@celery_app.task
def send_email():
print("正在发送email....")
time.sleep(5)
print("email发送完毕!")
@celery_app.task
def upload_file():
print("正在处理文件上传任务....")
time.sleep(3)
print("文件上传完毕!")
@celery_app.task
def image_process():
print("正在处理图像....")
time.sleep(5)
print("图像处理完毕!")
@celery_app.task
def period_task(a, b):
print("开始周期性任务...")
time.sleep(3)
print("周期性任务执行完毕:%d!" % (a + b))
启动woker处理周期性任务:
celery -A proj worker --loglevel=info --beat
在命令后面加上--beat,表示启动周期性任务。
如果我们想指定在某天某时某分某秒执行某个任务,可以执行cron任务, 增加配置信息如下:
CELERYBEAT_SCHEDULE = {
'every-5-minute':
{
'task': 'proj.tasks.period_task',
'schedule': 5.0,
'args': (16, 16),
},
'add-every-monday-morning': {
'task': 'proj.tasks.period_task',
'schedule': crontab(hour=7, minute=30, day_of_week=1),
'args': (16, 16),
},
}
crontab编写的例子:
例子 | 含义 |
crontab() | 每分钟执行 |
crontab(minute=0, hour=0) | Execute daily at midnight. |
crontab(minute=0, hour='*/3') | Execute every three hours: midnight, 3am, 6am, 9am, noon, 3pm, 6pm, 9pm. |
crontab(minute=0, | Same as previous. |
crontab(minute='*/15') | Execute every 15 minutes. |
crontab(day_of_week='sunday') | Execute every minute (!) at Sundays. |
crontab(minute='*', | Same as previous. |
crontab(minute='*/10', | Execute every ten minutes, but only between 3-4 am, 5-6 pm, and 10-11 pm on Thursdays or Fridays. |
crontab(minute=0, hour='*/2,*/3') | Execute every even hour, and every hour divisible by three. This means: at every hour except: 1am, 5am, 7am, 11am, 1pm, 5pm, 7pm, 11pm |
crontab(minute=0, hour='*/5') | Execute hour divisible by 5. This means that it is triggered at 3pm, not 5pm (since 3pm equals the 24-hour clock value of “15”, which is divisible by 5). |
crontab(minute=0, hour='*/3,8-17') | Execute every hour divisible by 3, and every hour during office hours (8am-5pm). |
crontab(0, 0, day_of_month='2') | Execute on the second day of every month. |
crontab(0, 0, | Execute on every even numbered day. |
crontab(0, 0, | Execute on the first and third weeks of the month. |
crontab(0, 0, day_of_month='11', | Execute on the eleventh of May every year. |
crontab(0, 0, | Execute on the first month of every quarter. |
开启一个celery beat服务:
celery -A proj beat
celery需要保存上次任务运行的时间在数据文件中,文件在当前目录下名字叫celerybeat-schedule. beat需要访问此文件:celery -A proj beat -s /home/celery/var/run/celerybeat-schedule