1、redis安装并启动redis安装与使用-CSDN博客
2、安装 celery 、eventlet 3. Task handler raised error: ValueError('not enough values to unpack (expected 3, got 0)') - Redskaber - 博客园
pip install celery pip install eventlet
3、python 版本3.10
#创建异步任务
# tasks.py import celery from celery import Celery import time app = Celery('tasks', broker='redis://127.0.0.1:6379/0', backend='redis://127.0.0.1:6379/1') @app.task def add(x, y): for i in range(10): time.sleep(1) print('lala') return x + y
命令行启动 在tasks路径下执行
celery -A tasks worker --loglevel=info
1、调用异步任务报错 Task handler raised error: ValueError('not enough values to unpack (expected 3, got 0)')
解决办法 安装 eventlet
celery -A tasks worker -l info -P eventlet
2、consumer: Cannot connect to redis://localhost:6379/0: Error 11001 connecting to localhost:6379. No address found
解决办法:localhost问题,redis链接修改为127.0.0.1
# 调用异步任务
from tasks import add
result = add.delay(4, 4)
print(result.id) # 打印任务ID
如何获取结果呢?
print(result.__dict__) # 结果信息 print(result.successful()) # 是否成功 print(result.failed()) # 是否失败 print(result.ready()) # 是否执行完成 print(result.state) # 状态 PENDING -> STARTED -> SUCCESS/FAIL print('here') # 你可以使用result.ready()来检查任务是否已经完成,然后使用result.get()获取结果。 value = result.get() # 任务返回值
celery -A tasks worker -l info -P eventlet
celery -A tasks worker --loglevel=info -P threads