芹菜发现所有任务,甚至当'app.autodiscover_tasks()`不叫(Celer

2019-11-04 20:05发布

我使用Django==2.0.5celery==4.0.2 。 我的proj/proj/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', '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))

我期待中没有一个与装饰的任务shared_tasktasks.py的应用程序将被发现的,但让我吃惊,大多数的任务可以下可见[tasks]运行时, celery workercelery worker -A proj -l INFO

我的目录结构有点像:

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_IMPORTS不设置settings.py和我都甚至试图CELERY_IMPORTS=()CELERY_IMPORTS=['path/to/one/of/the/modules']即使在当时所有的任务被发现的。

任何的建议是值得欢迎的。

Answer 1:

You should try @app.task without the (bind=True) flag. Also celery can detect all the tasks inside a module if they are in the init file of the module. In the django settings.py

CELERY_IMPORTS=("path/to/module",) 

will cause to discover everything in that module. Make sure you haven't done something similar.

Can you also share the project folder structure?



文章来源: Celery discovers all tasks even when `app.autodiscover_tasks()` is not called