views.py
from registration.models import Registration
from django.core.context_processors import csrf
from registration import tasks
from django.shortcuts import render_to_response
def validate(request):
state="notvalid"
if request.method == 'POST':# If the form has been submitted...
form = Registration(request.POST) # A form bound to the POST data
state="notvalid"
if form.is_valid():
username = form.cleaned_data['username']
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
email = form.cleaned_data['email']
password = form.cleaned_data['password']
from django.contrib.auth.models import User
user = User.objects.create_user(username,email,password)
user.first_name=first_name
user.last_name=last_name
user.save()
state="added data"
tasks.mail()
else:
form = Registration() # An unbound form
return render_to_response('auth1.html', {
'form': form,'state':state,
})
tasks.py
from celery import task
import celery
@celery.task()
def mail():
from django.core.mail import send_mail
for x in range(10000000):
print x
return True
the task has to be performed parrelel without affecting the time of views response. I.e
in background it has to run mail function
but after database adding it hase to return render_to_response
Your call to trigger the
mailtask should be:Without the
delay(), the task will run in the same process as the view, not in a Celery worker, and so the view function will block untilmailis finished.See http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#calling-our-task for more.