为什么Celery只识别来自单个Django应用的任务

时间:2018-10-22 08:35:01

标签: python django celery django-celery

因此,我们有一个包含多个应用程序的Django项目,并且我们将celery用于任务。我们遇到的问题是只有一个应用程序的task.py内部的任务将运行,其他应用程序中的其他task.py任务将返回以下错误:

declare(strict_types=1);

当我运行test_task.delay()时会发生这种情况

这是供应商的task.py:

celery_1     | [2018-10-22 08:27:59,563: ERROR/MainProcess] Received unregistered task of type 'biko.supplier.tasks.test_task'.
celery_1     | The message has been ignored and discarded.
celery_1     |
celery_1     | Did you remember to import the module containing this task?
celery_1     | Or maybe you're using relative imports?
celery_1     |
celery_1     | Please see
celery_1     | http://docs.celeryq.org/en/latest/internals/protocol.html
celery_1     | for more information.
celery_1     |
celery_1     | The full contents of the message body was:
celery_1     | b'[[], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (77b)
celery_1     | Traceback (most recent call last):
celery_1     |   File "/usr/local/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 557, in on_task_received
celery_1     |     strategy = strategies[type_]
celery_1     | KeyError: 'biko.supplier.tasks.test_task'

这是商店task.py的一部分,任务可以正常运行:

from config.celery import app

@app.task(shared=True)
def test_task():
    print("Runnign this task correctly")

这是芹菜配置:

from django.contrib.contenttypes.models import ContentType

from config.celery import app
from celery_once import QueueOnce
from django.core.management import call_command
from django.utils import timezone
from raven.contrib.django.raven_compat.models import client

from biko.shop.models import Shop
from config.settings import MAX_INCOMING_BUFFER_RETRIES
from biko.buffer.models import IncomingBuffer, OutgoingBuffer

@app.task(shared=True)
def buffer_products(shop_id):
    shop = Shop.objects.get(id=shop_id)
    shop.get_manager().buffer_products()

任何不属于shop / tasks.py的任务都不会显示为正在加载。我不知道为什么它会从shop / tasks.py而不是从另一个应用程序加载任务。

1 个答案:

答案 0 :(得分:2)

在Celery配置中;您可以执行以下操作:

# Where app_module represents where tasks exists.
app = Celery('biko', include=['app_module.tasks'])

# Your line should also work, but sometimes it needs the apps configs
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)