一段时间以来,我一直在尝试创建一个任务,该任务包括每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中创建这一行并从.view导入时,视图便会出现。
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'
答案 0 :(得分:0)
您没有分享您的settings.py
或您如何经营芹菜工人,所以我大胆猜测。
我建议您通过celery's user guide。我认为它可以进行一些结构上的改进,但足以理解基础知识。
答案 1 :(得分:0)
要扩展@gokhan的答案,您需要确保两件事:
@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()
Muestras
中出现settings.INSTALLED_APPS
。 This will allow the autodiscover
to discover your tasks:接下来,可重用应用程序的常见做法是在单独的task.py模块中定义所有任务,而Celery确实有一种自动发现这些模块的方法:
app.autodiscover_tasks()
在Celery上方的行中,将按照task.py约定自动从所有已安装的应用程序发现任务: