Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 芹菜发现所有任务,即使未调用'app.autodiscover_tasks()'_Django_Celery_Celery Task - Fatal编程技术网

Django 芹菜发现所有任务,即使未调用'app.autodiscover_tasks()'

Django 芹菜发现所有任务,即使未调用'app.autodiscover_tasks()',django,celery,celery-task,Django,Celery,Celery Task,我使用的是Django==2.0.5和芹菜==4.0.2。 我的proj/proj/芹菜.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_SETTIN

我使用的是
Django==2.0.5
芹菜==4.0.2
。 我的
proj/proj/芹菜.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', 'proj.settings')

app = Celery('proj', include=[])

# 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('Request: {0!r}'.format(self.request))
我原以为在
tasks.py
的应用程序中不会发现任何用
shared\u task
修饰的任务,但令我惊讶的是,当使用
芹菜工人-A proj-l INFO
运行
芹菜工人
时,大多数任务都可以在
[tasks]
下看到

我的目录结构有点像:

app
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── constants.py
│   ├── scripts
│   │   ├── __init__.py
│   ├── factories.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   ├── models.py
│   ├── tasks.py
│   └── tests
│       ├── __init__.py
CELERY\u IMPORTS
未在
settings.py中设置,我甚至尝试了
CELERY\u IMPORTS=()
CELERY\u IMPORTS=['path/to/one/of/the/modules']
,即使所有任务都被发现了


欢迎任何建议。

您应该尝试@app.task,但不带(bind=True)标志。 此外,如果芹菜在模块的init文件中,芹菜可以检测模块内的所有任务。在django settings.py中

CELERY_IMPORTS=("path/to/module",) 
将导致发现该模块中的所有内容。确保你没有做过类似的事情


您还可以共享项目文件夹结构吗?

我在使用您现在使用的芹菜和django的相同版本时遇到了相同的问题,请以完全相同的方式在其他地方重新创建项目,并尝试检测任务。
(bind=False)
没有任何效果,我已使用您要求的信息更新了问题。我已在三个不同的环境中使用相同的源代码进行了尝试,但结果仍然是一样的。我在我的一个项目中成功地再现了该问题,并且检测到了一些任务,尽管在CELERY_导入或celeryconfig文件中未提及。如果您找到了解决方案,请与我们分享。另外,如果你删除或注释掉这些任务,它们将不会出现,但它们仍然会被缓存在芹菜中的某个地方,我想,因为一旦你注释掉它们,它们就会重新出现。也许这是一些默认行为,但这样做没有多大意义。
芹菜
有时会缓存东西,你应该尝试重新加载使用的守护进程,或者芹菜本身。我在三个不同的服务器上尝试了这一点,重新加载了好几次。