django - 类型错误 : autodiscover_tasks() missing 1 required positional argument: 'packages' . celery

标签 django celery

我正在采取第一步在我的 Django 应用程序的后台添加 Celery 任务。但我仍然收到错误。

  File "C:\Users\tymot\Desktop\send_sms_oferia\app_rama\app_rama\__init__.py", line 5, in <module>
    from .celery import app as celery_app
  File "C:\Users\tymot\Desktop\send_sms_oferia\app_rama\app_rama\celery.py", line 17, in <module>
    app.autodiscover_tasks()
TypeError: autodiscover_tasks() missing 1 required positional argument: 'packages'

这个错误来自哪里?我该如何解决?

我尝试按照此 documentation 执行我的步骤,所以我的文件看起来像这样:
celery.py(在我的项目目录中):
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

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

app = Celery('app_rama')

# 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')

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


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

我的 __init__.py文件(在我的项目目录中)
from __future__ import absolute_import, unicode_literals

# 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',)

最佳答案

尝试将已安装的应用程序传递给 autodiscover_tasks像这样:

from django.conf import settings
app.autodiscover_tasks(settings.INSTALLED_APPS)

Check the documentation on this function

关于django - 类型错误 : autodiscover_tasks() missing 1 required positional argument: 'packages' . celery ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56788736/

相关文章:

python celery : Retrieve tasks arguments if there's an exception

Django ManyToMany 验证约束

python - DJANGO:表单中缺少空白选择选项

python - 关于在工作节点上分发 python 数据/代码的建议?

python - 启动 celery Worker 并启用它的广播队列

python - 路径错误 ImportError : attempted relative import with no known parent package

python - 在 Django 单元测试中使用 mock 修补 celery 任务

django-tastypie 让每个数据库查询两次

django - 如何在Django中使用ForeignKey初始化一个空对象?

python - 无法从 django_pagseguro.urls 导入 pagseguro_urlpatterns