简体   繁体   English

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

[英]Why does Celery only recognize tasks from single Django app

So we have this Django project with multiple apps, and we use celery for tasks. 因此,我们有一个带有多个应用程序的Django项目,并且我们将celery用于任务。 The issue we are running into is that only the tasks inside the tasks.py of a single app will run, other tasks.py tasks in other apps return the following error: 我们遇到的问题是只有一个应用程序的task.py内部的任务将运行,其他应用程序中的其他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'

This happens when I run test_task.delay() 当我运行test_task.delay()时会发生这种情况

Here is the supplier tasks.py: 这是供应商task.py:

from config.celery import app

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

Here is the a part of the shop tasks.py, where the tasks do work correctly: 这是shop task.py的一部分,任务可以正常运行:

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()

And here is the celery config: 这是芹菜配置:

import os

from celery import Celery
from celery.schedules import crontab
from django.conf import settings

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

app = Celery('biko')

app.config_from_object('django.conf:settings')

app.autodiscover_tasks()
app.conf.ONCE = {
  'backend': 'celery_once.backends.Redis',
  'settings': {
    'url': 'redis://redis',
    'blocking': True,
    'default_timeout': 60 * 60,
    'blocking_timeout': 86400
  }
}

Any task not part of shop/tasks.py is not shown as being loaded. 不属于shop / tasks.py的任何任务都不会显示为正在加载。 I have no clue as to why it would load tasks from shop/tasks.py but not from another app. 我不知道为什么它会从shop / tasks.py而不是从另一个应用程序加载任务。

In Celery config; 在芹菜配置中; You can do the following: 您可以执行以下操作:

# 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM