【问题标题】:django celery import function not workingdjango celery 导入功能不起作用
【发布时间】:2019-04-07 06:45:33
【问题描述】:

我一直在尝试创建一项任务,其中包括每 5 小时创建一个样本样本。我已经设法用 redis 配置 celery 并执行文档中作为示例的任务,但是当我想做一些更复杂的事情时,它不会执行我。重新启动队列时任务会从列表中消失.

这是项目的结构:

    proj:
  Muestras:
    -views.py
    -tasks.py
    -models.py
  Servicios:
    -models.py
  proj:
    -celery.py
    -settings.py

在settings.py中:

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/London'
CELERY_BEAT_SCHEDULE = {
   'generar-muestras': {  # name of the scheduler
   'task': 'Muestras.tasks.crear_muestras_tarea',
   'schedule': 30.0,  # set the period of running
  },
}

这是 Muestras.views 中的视图

from .models import Muestra
from backend.Servicios.models import Servicio

#this works in console
def generar_muestras():
    services = Servicio.models.all()

    for i in services:
        muestra = Muestra(servicio_id=i.id)
        muestra.save

在 Muestras.tasks.py 中

from __future__ import absolute_import, unicode_literals
from celery import task
from .views import generar_muestras


 @task
 def crear_muestras_task():
     print('hola esto tiene una funcion')

     #generar_muestras()

这就是我在 celery.py 中的内容:

 from __future__ import absolute_import, unicode_literals
 import os

 from celery import Celery
 from django.conf import setting

 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

 app = Celery('proj')
 app.config_from_object('django.conf:settings', namespace='CELERY')
 app.autodiscover_tasks()

 @app.task(bind=True)
 def debug_task(self):
     print('Request: {0!r}'.format(self.request))

以及何时执行

celery -A proj worker -l info -B

一切正常并执行任务,但是当我在 Muestras.tasks.py 中创建这一行并从 .views 导入视图时。

         generar_muestras()

任务从列表中消失,我收到此错误:

[2018-11-04 22:31:37,734: INFO/MainProcess] celery@linux-z6z3 ready.
[2018-11-04 22:31:37,876: ERROR/MainProcess] Received unregistered task of 
type 'Muestras.tasks.crear_muestras_tarea'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you're using relative imports?

Please see
http://docs.celeryq.org/en/latest/internals/protocol.html
for more information.

The full contents of the message body was:
b'[[], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": 
null}]' (77b)
Traceback (most recent call last):
File "/home/wecbxxx/PycharmProjects/porj/venv/lib64/python3.6/site- 
packages/celery/worker/consumer/consumer.py", line 558, in 
on_task_received
strategy = strategies[type_]
KeyError: 'Muestras.tasks.crear_muestras_tarea'

【问题讨论】:

    标签: django task celery


    【解决方案1】:

    您没有分享您的 settings.py 或您如何管理 celery worker,所以我在胡乱猜测。

    • 你的任务应该列在imports celery 的设置下。 See here

    • 您的任务应该由@app.task() 装饰。 See here

    我建议你通过celery's user guide。我认为它可以使用一些结构改进,但应该足以理解基础知识。

    【讨论】:

      【解决方案2】:

      要扩展@gokhan 的答案,您应该确保两件事:

      1. @app.task装饰你的任务
      
      from __future__ import absolute_import, unicode_literals
      from proj.celery import app 
      from .views import generar_muestras
      
      
      @app.task
      def crear_muestras_task():
          print('hola esto tiene una funcion')
          #generar_muestras()
      
      1. 确保Muestras 出现在settings.INSTALLED_APPS 中。 This will allow the autodiscover to discover your tasks:

      接下来,可重用应用程序的一个常见做法是在单独的 tasks.py 模块中定义所有任务,Celery 确实有办法自动发现这些模块:

      app.autodiscover_tasks()

      根据 tasks.py 约定,Celery 将自动从所有已安装的应用中发现任务:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-07
        • 1970-01-01
        • 2018-11-12
        • 2018-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-17
        相关资源
        最近更新 更多