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函数