普通的worker是为了执行异步任务,如果想开启定时任务,则需要启动beat服务
具体配置流程如下:
- CELERY = {
- "broker_url": "***",
- "result_backend": "***",
- # 定时任务
- 'imports': (
- 'apps.tasks.scheduled_task',
- ),
- "beat_schedule": {
- 'per_5_sec': {
- 'task': 'test_func',
- "schedule": datetime.timedelta(seconds=5),
- "args": (),
- },
- },
- }
参考以上配置,则需要在apps.tasks.scheduled_task.py下,创建test_func
- @celery.task(name='test_func')
- def test_func():
- print('{}-test_func -- print'.format(datetime.datetime.now()))
- #!/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