35

Let's take a simple Django example.

app/models.py

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    token = models.CharField(max_length=32)

app/views.py

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from forms import RegisterForm
from utils.utilities import create_user

@csrf_exempt
def register_view(request):
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            create_user(form.cleaned_data)
            return HttpResponse('success')

utils/utilities.py

def create_user(data):
    user = User.objects.create_user(username=data['username'], email=None, password=data['password'])
    user.save()
    profile = UserProfile()
    profile.user = user
    profile.token = generate_token()
    profile.save()

Can somebody provide an implementation of Celery in this example? Imagine this is a large project with hundreds of requests per sec.

Teodor Scorpan
  • 838
  • 1
  • 9
  • 17
  • 1
    Just so you know, direct requests for examples tend to be regarded as off-topic, as they are frequently come from people who've not done any prior research. That clearly doesn't apply to you, but it's worth being aware of that general guideline - see the [Help](http://stackoverflow.com/help) section when you get a mo. – halfer Nov 27 '13 at 20:15

1 Answers1

79

Assuming you have both Python's celery and django-celery installed, create the following tasks.py file under your app:

utils/tasks.py

from celery import task
# other imports

@task()
def create_user(data):
    user = User.objects.create_user(
        username=data['username'], email=None, password=data['password']
    )
    user.save()
    profile = UserProfile()
    profile.user = user
    profile.token = generate_token()
    profile.save()

    return None

Delete your utils/utilities.py file in your example above.

In your code in views.py change the create_user call from:

create_user(form.cleaned_data)

to:

create_user.delay(form.cleaned_data)

Basically create_user is now a celery task; if you have the right Python packages installed (as mentioned above), code-wise (the implementation you ask for) that's it. delay executes your function asynchronously - i.e. the HTTP response is returned without waiting for the asynchronous task to complete.

Locally you can run a celery daemon process using python manage.py celeryd.

In production you have to set up the celery process itself using for instance upstart, supervisor or any other tool to control the lifecycle of such process.

Further details documented here.

Joseph Victor Zammit
  • 13,080
  • 9
  • 66
  • 98