我有使用 Celery 4.4.2 版的 Django 应用程序,它运行良好。
from celery import task
import logging
@task(ignore_result=True)
def log_user_activity(user_id):
try:
logging.info(user_id)
except Exception as e:
logging.error(str(e))
当我尝试将 Celery 版本更新到 v5.2.2 时,出现以下错误:
ImportError: cannot import name 'task' from 'celery'
有人可以帮助替换任务吗? 他们仍然有同样的例子。 https://github.com/celery/celery/blob/v5.2.2/examples/celery_http_gateway/tasks.py
最佳答案
此 API 已被弃用,然后 removed in 5.0 .
该页面建议更改
from celery import task
进入
from celery import shared_task
还有其他更改不适用于您发布的代码段,但可能适用于您的其余代码。有关详细信息,请参阅该页面(以及文档的其余部分,尤其是 Upgrading from Celery 4.x 部分)。
关于python - Django,导入错误 : cannot import name 'task' from 'celery' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70649916/