Django & Celery – Easy async task processing

So, while developing a web application, there comes a time when we need to process some of the tasks in the background, perhaps asynchronously. For example, your user would upload photos and the app would post them to multiple social networks. We would definitely want to offload the uploading task to some background workers.

Django and Celery makes background task processing a breeze. In this article, we shall see how we can setup Django and Celery to start processing our background tasks. We would use Redis to maintain our task queue.

How does it work?

  • We define some tasks in our application. These tasks are expected to run for a pretty long time.
  • We run the celery workers. Celery knows how to find and load these tasks. The workers keep waiting on us.
  • We add some jobs to the workers queue from our web app. The workers now have something to work on. So they start taking the jobs from the queue and start processing them.
  • We can query the status of the jobs from our web app to know whats happening.
  • The easy to use Python API makes it really simple to use. You don’t need any specialisation or anything in Redis.

Setting Up

Let’s first install the Redis server:

The version that comes from Ubuntu official repo is quite old. You can install the latest version from 3rd party PPAs.

Install Celery with Redis support:

And then install django-celery package:

Configuration

Add “djcelery” to your installed apps list:

Modify your main app’s settings.py file to add the celery specific settings:

Now, inside your main application directory (the directory in which settings.py is located), create a file named “celery.py” with these contents:

The above codes do a few things:

  • It creates our own Celery instance.
  • We ask the celery instance to load necessary configs from our project’s settings file.
  • We make the instance auto discover tasks from our INSTALLED_APPS.

Also let’s modify the “__init__.py” file in the same directory to make the celery app available more easily:

This would allow us to use the same app instance for shared tasks across reusable django apps.

Defining Tasks

Now let’s create a tasks.py file in one of our INSTALLED_APPS and add these contents:

Now we have defined our own celery app, we have our tasks. It’s now time to launch the workers and start adding tasks.

Processing Tasks

Before we can start processing tasks, we have to launch the celery daemon first. This is how we do it:

Here, we tell celery to use the celery instance we defined and configured earlier. Here “project” is the main app, the package that contains our settings.py along with celery.py. The “app” the variable name which holds the celery instance.

Now let’s use the Django shell to add and query jobs:

So as we can see, out task was processed by celery. And we could easily query the status. We would generally use the meta data to store any task related information.


2 Comments Django & Celery – Easy async task processing

  1. Dharmit

    Hi Masnun,

    Thanks for writing up this blog about celery.

    I am facing below error in the last step:

    [2015-05-06 12:07:43,928: ERROR/MainProcess] Received unregistered task of type u’rest_apis.tasks.UploadTask’.
    The message has been ignored and discarded.

    Did you remember to import the module containing this task?
    Or maybe you are using relative imports?
    Please see http://bit.ly/gLye1c for more information.

    The full contents of the message body was:
    {u’utc’: True, u’chord’: None, u’args’: [u’hello world!’], u’retries’: 0, u’expires’: None, u’task’: u’rest_apis.tasks.UploadTask’, u’callbacks’: None, u’errbacks’: None, u’timelimit’: [None, None], u’taskset’: None, u’kwargs’: {}, u’eta’: None, u’id’: u’31ef9b3c-afca-442b-9217-6e6db2b1ad40′} (277b)
    Traceback (most recent call last):
    File “/path/to/venv/local/lib/python2.7/site-packages/celery/worker/consumer.py”, line 455, in on_task_received
    strategies[name](message, body,
    KeyError: u’rest_apis.tasks.UploadTask’

     

    I tried Google’ing around and tried few suggestions. But it didn’t help. Can you suggest a way to solve this issue?

     

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *