【问题标题】:TypeError: autodiscover_tasks() missing 1 required positional argument: 'packages'. Celery类型错误:autodiscover_tasks() 缺少 1 个必需的位置参数:“包”。芹菜
【发布时间】:2019-11-09 08:09:21
【问题描述】:

我正在迈出第一步,在我的 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',)

【问题讨论】:

    标签: django celery


    【解决方案1】:

    尝试将已安装的应用程序传递给autodiscover_tasks,如下所示:

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

    Check the documentation on this function

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 2018-09-12
      • 2021-08-05
      • 2021-07-06
      • 2021-08-05
      • 2017-07-23
      相关资源
      最近更新 更多