1.运行redis
2.安装celery:pip install celery[redis]
3.vim task.py
import time from celery import Celery broker = 'redis://localhost:6379/1' backend = 'redis://localhost:6379/2'
app = Celery("my_task",broker=broker,backend=backend) #my_task 是用来生成任务名的前缀
@app.task
def add(x,y):
print("enter call func....")
time.sleep(4)
return x + y
4.vim app.py
import time from task import add if __name__ == '__main__': print("start task....") result = add.delay(10,20) print(result)
5.启动celery worker
celery worker -A task【任务文件名】 -l INFO
6.运行app.py或者执行add函数
python下:
启动worker:celery worker -A celery_app -l INFO
启动定时任务:celery beat -A celery_app -l INFO
用一条命令同时启动worker和定时任务:celery -B -A celery_app worker -l INFO
djangp下:
启动worker:python manage.py celery worker -l INFO
启动定时任务:python manage.py celery beat -l INFO
使用flower监控任务:pip install flower
启动:python manage.py celery flower
认证启动:python manage.py celery flower --basic_auth=chenjiaen:root123456.
打开页面连接:http://115.159.118.25:5555
使用 supervisor 管理进程
生成配置文件:echo_supervisord_conf > conf/supervisord.conf