gpt4 book ai didi

python - Django 和 Celery - ModuleNotFoundError : No module named 'celery.task'

转载 作者:行者123 更新时间:2023-12-04 11:08:30 31 4
gpt4 key购买 nike

我想在 Django 开始一个周期任务使用 Celery .以下是我的项目的相关部分:

# celery.py

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bookProjectSetting.settings')

app = Celery('bookProjectSetting')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
__init__.py看起来像这样:
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ('celery_app',)
我的 tasks.py文件在我的 books应用程序文件夹如下所示:
from books.models import Book
from celery.schedules import crontab
from celery.decorators import periodic_task

@periodic_task(
run_every=(crontab(minute='*/10')),
name="update_life_time_of_books",
ignore_result=True)
def update_life_time_of_books():
# do sth.
我也设置了 redis作为经纪人并安装了 django-celery-beat东西。但是当我通过命令运行工作程序时:
celery -A bookProjectSetting beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler
然后我收到以下错误:
File "/home/ac3l1k/Desktop/PROJECT/simpleDjangoProjects/bookProject/env/lib/python3.8/site-packages/celery/local.py", line 403, in _compat_periodic_task_decorator
from celery.task import periodic_task
ModuleNotFoundError: No module named 'celery.task'
从错误中我假设装饰器有问题,但我不知道为什么。希望有人能帮忙。
提前致谢:=)
注意:这是我的 requirements.txt 的内容版本引用文件 $ pip freeze
amqp==5.0.1
asgiref==3.2.10
billiard==3.6.3.0
celery==5.0.1
click==7.1.2
click-didyoumean==0.0.3
click-repl==0.1.6
Django==3.1.2
django-celery-beat==2.1.0
django-timezone-field==4.0
djangorestframework==3.12.1
kombu==5.0.2
prompt-toolkit==3.0.8
python-crontab==2.5.1
python-dateutil==2.8.1
pytz==2020.1
redis==3.5.3
six==1.15.0
sqlparse==0.4.1
vine==5.0.0
wcwidth==0.2.5

最佳答案

“任务”不再出现在 5.x 版本中
您可以使用 4.x 版

  • pip3 卸载 celery
  • pip3 安装 celery ==4.4.2

  • 修改了空格

    关于python - Django 和 Celery - ModuleNotFoundError : No module named 'celery.task' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64483648/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com