普通的worker是为了执行异步任务,如果想开启定时任务,则需要启动beat服务
具体配置流程如下:
1、配置config
CELERY = {
"broker_url": "***",
"result_backend": "***",
# 定时任务
'imports': (
'apps.tasks.scheduled_task',
),
"beat_schedule": {
'per_5_sec': {
'task': 'test_func',
"schedule": datetime.timedelta(seconds=5),
"args": (),
},
},
}
2、在对应路径下创建func
参考以上配置,则需要在apps.tasks.scheduled_task.py下,创建test_func
@celery.task(name='test_func')
def test_func():
print('{}-test_func -- print'.format(datetime.datetime.now()))
3、创建启动celery的shell
#!/bin/bash
log_path="/日志路径/"
worker_file="celery.log"
schedule_file="celerybeat-schedule"
worker_file_path=$log_path$worker_file
schedule_file_path=$log_path$schedule_file
celery -A apps.celery_app:app worker -l info -c 3 -f $worker_file_path &
celery -A apps.celery_app:app beat -l info -s $schedule_file_path
本文介绍了如何使用Celery配置和启动定时任务。首先,在配置文件中设置broker_url和result_backend,并添加定时任务入口。然后,在指定模块创建任务函数。最后,通过shell脚本启动Celery worker和beat服务来执行定时任务。


被折叠的 条评论
为什么被折叠?



