DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Frameworks
  4. Django in Production - Part 2 - Background Tasks

Django in Production - Part 2 - Background Tasks

Rob Golding user avatar by
Rob Golding
·
Jan. 23, 12 · Interview
Like (0)
Save
Tweet
Share
5.44K Views

Join the DZone community and get the full member experience.

Join For Free

This is the second part in a series about Django in Production.

In my last post, I described in detail the core stack which powers a Django application in production. In this second part, I’ll talk about something which has become commonplace in modern web applications - background tasks.

Often websites perform complex tasks which take a long time to finish. These tasks don’t always need to be done before the response is sent to the browser, so can be executed elsewhere. In cases like this, tasks can be performed asynchronously, by one or more “workers” which consume a task queue.

Celery

The most common task queue used with Django, at least in my experience, is called Celery.

Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well.


Celery isn’t dependant on Django in any way - it’s a completely separate application. It does, however, integrate extremely well with Django projects. By installing the django-celery package and then adding djcelery to the list of INSTALLED_APPS, Celery can be run and managed through Django itself - thanks to a set of handy management commands.

This post isn’t about how to get Celery working, so I’ll skip over the details. For a more information on using Celery with Django, the project’s documentation is excellent.

Celery In Production

Running Celery locally is easy: a simple python manage.py celeryd does the trick. In production, though, something a little more robust is needed.

Thankfully, it’s not difficult at all. Just like Gunicorn, Celery can be overseen by Supervisor.

To add Celery to our Supervisor configuration, the following is all that’s required:

[program:app_name_celery_worker]
command=/path/to/env/bin/python manage.py celeryd -E -l info
directory=/path/to/app/
user=www-data
autostart=true
autorestart=true
stdout_logfile=/path/to/app/django_logs/celeryd.log
redirect_stderr=true

 

In fact, Celery even comes with some example Supervisor configuration files, which can be adapted to suit the particular needs of a production setup.

This configuration can be added to the same file which contains the application server’s program directives. This keeps all the daemons for an application neatly together in one file.

Worker Nodes

In development, a Celery worker would typically be spun up in another terminal window, alongside the development server. This same technique can’t be assumed though, for a production setup.

At first, perhaps, the task queue worker(s) might be running on the same server as the application itself. As an application grows, there comes a point where it makes sense to have at least one server dedicated to running asynchronous tasks. This can then be scaled out to suit, by adding more and more worker servers as required.

RabbitMQ

Celery uses message passing to queue tasks for the workers to consume. This is provided by an altogether separate application - the message broker.

Celery’s most commonly used message broker, and that with the most support, is RabbitMQ. The details of how to setup RabbitMQ are a little out of the scope of this post, but the server documentation are complete and provide a good starting point.

Virtual Hosts

In a large application with more than one worker node, the message broker would typically sit on a dedicated host (or even be clustered over multiple hosts). In this case, multiple applications might share the same message broker.

RabbitMQ supports this type of configuration through the use of virtual hosts (or VHosts). Much like virtual hosts used in Apache, these allow a single broker to be “split” into multiple, distinct parts. Access control can be configured on a per-vhost basis, so any single application is prevented from affecting another through the use of a shared broker.

Even if your message broker is only used by one application, that application should be configured with its own virtual host and credentials.

Monitoring

It’s difficult to know exactly what a task queue like Celery is doing, as it doesn’t present a pretty web interface for us to watch. It can, however, be monitored - and this is something I’ll be talking about in detail in the next post of this series.



Source: http://www.robgolding.com/blog/2011/11/27/django-in-production-part-2---background-tasks/

Task (computing) Django (web framework) Production (computer science) application Celery (software)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Low-Code Development: The Future of Software Development
  • How To Use Linux Containers
  • What Is Continuous Testing?
  • File Uploads for the Web (1): Uploading Files With HTML

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: