芹菜自动发现任务不适用于所有Django 1.7应用程序。

时间:2021-06-15 19:18:38

I have a Django 1.7 project with Celery 3.1. All the apps in my Django project work with the new AppConfig. The problem is that not all the tasks are found with autodiscover_tasks:

我有一个Django 1.7项目和芹菜3.1。我的Django项目中的所有应用都使用新的AppConfig。问题是,并不是所有的任务都是通过autodiscover_tasks找到的:

app.autodiscover_tasks(settings.INSTALLED_APPS)

If i use autodiscover_tasks like this it wil work:

如果我使用autodiscover_tasks,它会工作:

app.autodiscover_tasks(settings.INSTALLED_APPS + ('apps.core','apps.sales'))

The tasks defined in websites are found but the tasks in core and sales are not. All have the same layout with apps.py and tasks.py.

在网站上定义的任务被发现,但核心和销售的任务不是。所有的应用程序都有相同的布局。py和tasks.py。

The project folder structure is:

项目文件夹结构为:

apps
  core
  apps.py
  tasks.py
dashboard
  apps.py
sales
  apps.py
  tasks.py
websites
  apps.py
  tasks.py

The class definitions are as follows:

类定义如下:

class WebsitesConfig(AppConfig):
    name = 'apps.websites'
    verbose_name = 'Websites'

class SalesConfig(AppConfig):
    name = 'apps.sales'
    verbose_name = 'Sales'

3 个解决方案

#1


14  

This is discussed in a number of Celery issues, such as #2596 and #2597.

这里讨论了一些芹菜问题,如#2596和#2597。

If you are using Celery 3.x, the fix is to use:

如果你用的是芹菜。x,修复方法是:

from django.apps import apps
app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])

As mentioned in #3341, if you are using Celery 4.x (soon to be released) you can use:

正如#3341中提到的,如果你使用的是芹菜4。x(即将发布)您可以使用:

app.autodiscover_tasks()

#2


0  

I had to add this to the module where my celery app was defined:

我必须把这个添加到我的芹菜应用程序定义的模块中:

from __future__ import absolute_import

#3


0  

I just had this problem because of a misconfigured virtual environment.

我之所以遇到这个问题,是因为虚拟环境配置不当。

If an installed app has a dependency missing from the virtual environment in which you're running celery, then the installed app's tasks will not be auto discovered. This hit me as I was moving from running my web server and celery on the same machine to a distributed solution. A bad build resulted in different environment files on different nodes.

如果安装的应用程序在运行芹菜的虚拟环境中缺少依赖项,那么安装的应用程序的任务就不会自动被发现。当我从运行我的web服务器和芹菜到一个分布式的解决方案时,我突然想到了这一点。一个糟糕的构建导致了不同节点上的不同环境文件。

I added the dependencies that were missing then restarted the celery service.

我添加了缺少的依赖项,然后重新启动了芹菜服务。

#1


14  

This is discussed in a number of Celery issues, such as #2596 and #2597.

这里讨论了一些芹菜问题,如#2596和#2597。

If you are using Celery 3.x, the fix is to use:

如果你用的是芹菜。x,修复方法是:

from django.apps import apps
app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])

As mentioned in #3341, if you are using Celery 4.x (soon to be released) you can use:

正如#3341中提到的,如果你使用的是芹菜4。x(即将发布)您可以使用:

app.autodiscover_tasks()

#2


0  

I had to add this to the module where my celery app was defined:

我必须把这个添加到我的芹菜应用程序定义的模块中:

from __future__ import absolute_import

#3


0  

I just had this problem because of a misconfigured virtual environment.

我之所以遇到这个问题,是因为虚拟环境配置不当。

If an installed app has a dependency missing from the virtual environment in which you're running celery, then the installed app's tasks will not be auto discovered. This hit me as I was moving from running my web server and celery on the same machine to a distributed solution. A bad build resulted in different environment files on different nodes.

如果安装的应用程序在运行芹菜的虚拟环境中缺少依赖项,那么安装的应用程序的任务就不会自动被发现。当我从运行我的web服务器和芹菜到一个分布式的解决方案时,我突然想到了这一点。一个糟糕的构建导致了不同节点上的不同环境文件。

I added the dependencies that were missing then restarted the celery service.

我添加了缺少的依赖项,然后重新启动了芹菜服务。