1

I am trying to create some asynchronous tasks with celery in my django application

settings.py

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

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:

from __future__ import absolute_import
from .celery import app as celery_app

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. 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

Thanks in advance

2 Answers 2

1

Same problem with me. Task is working in python shell but it is not working in Django views.

I followed this 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.

app.config_from_object('django.conf:settings', namespace='CELERY')
Sign up to request clarification or add additional context in comments.

Comments

0

Check if redis is running

$redis-cli ping

Check if the celery worker is running in the django admin interface

If not running execute this command

celery -A provcon worker -l info

Test your task from your django application

If works run the celery worker in background like a daemon

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.