【发布时间】: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'
【问题讨论】: