简体   繁体   English

DJANGO celery任务是从外壳执行的,但不是从视图执行的

[英]DJANGO celery task is executed from shell but it's not executed from view

I am trying to create some asynchronous tasks with celery in my django application 我正在尝试在Django应用程序中使用celery创建一些异步任务

settings.py settings.py

BROKER_URL = 'django://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

celery.py: celery.py:

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

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

app = Celery('provcon')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

project __init__py: 项目__init__py:

from __future__ import absolute_import
from .celery import app as celery_app

tasks.py: tasks.py:

from __future__ import absolute_import
from celery import shared_task
from celery import task
from .models import Proc_Carga
@task()
def carga_ftp():
    tabla = Proc_Carga()
    sp = tabla.carga()

    return None

I call the asysnchronous tasks from my view like this: 我从我的视图中调用异步任务,如下所示:

from .tasks import carga_ftp

@login_required(login_url='/login/')
def archivoview(request):
    usuario= request.user
    if request.method == 'POST':
       form = ProcFTPForm(usuario,request.POST)
       if form.is_valid():
          form.save()
          proc = Lista_Final()
          lista = proc.archivos()
          # call asynchronous task 
          carga_ftp.delay()
          return HttpResponseRedirect('/resumen/')
    else:
      form = ProcFTPForm(usuario)
    return render_to_response('archivo.html',{'form':form},context_instance=RequestContext(request))

When I run the task from the python manage.py shell. 当我从python manage.py shell运行任务时。 The worker is executed and create the database objects without any problem 执行工作程序并创建数据库对象没有任何问题

but when I try to execute the task from the view, doesn't work is not executed 但是当我尝试从视图执行任务时,不执行不执行

Any idea why the task run from the manage.py shell but not from the views 知道为什么任务从manage.py shell运行而不是从视图运行

Thanks in advance 提前致谢

Check if redis is running 检查Redis是否正在运行

$redis-cli ping

Check if the celery worker is running in the django admin interface 检查celery worker是否正在django管理界面中运行

If not running execute this command 如果未运行,请执行以下命令

celery -A provcon worker -l info

Test your task from your django application 从Django应用程序测试您的任务

If works run the celery worker in background like a daemon 如果工作正常,则像后台程序一样在后台运行芹菜工人

Same problem with me. 我也有同样的问题。 Task is working in python shell but it is not working in Django views. Task在python shell中工作,但在Django视图中不工作。

I followed this http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html 我遵循了这个http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

I was missing namespace in app.config_from_object, after adding namespace='CELERY', It is working fine. 添加命名空间='CELERY'后,我在app.config_from_object中缺少命名空间,它工作正常。

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

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

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