普通的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