Text of realpython.com › asynchronous-tasks-with-django-and-celery as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Asynchronous Tasks With Django and Celery – Real Python

Asynchronous Tasks With Django and Celery by Martin Breuss Updated Oct 18, 2026 Reading time estimate 34m advanced databases django web-dev Table of Contents Remove ads Recommended Course Creating Asynchronous Tasks With Celery and Django (51m) Celery is a distributed task queue that processes tasks asynchronously, so slow work doesn’t hold up your web app’s response. When you integrate Celery with your Django application, you can offload time-consuming tasks and keep your users from waiting.

With Celery and Django, you can handle jobs like sending emails , processing images, and analyzing data without slowing down your application. By the end of this tutorial, you’ll understand that: Celery is a distributed task queue that handles tasks outside the main Django app flow. Celery workers take care of offloading work , while Celery beat handles scheduling tasks . Using Celery in Django helps maintain app responsiveness during time-intensive tasks.

Configuring Celery in Django involves setting up a message broker and defining tasks . Running a task in Celery requires calling the task with .delay() or .apply_async() . Celery is not a message queue but uses a message broker like Redis for communication. You’re in the right place if you’ve never used Celery in a Django app before, or if you’ve peeked into Celery’s documentation but couldn’t find your way around.

You’ll learn how to configure Celery in Django to handle tasks asynchronously so that your application stays responsive. If you’re on Django 6.0 or later, then you’ll also see how Django’s built-in Tasks framework compares to Celery, so you can pick the right tool for your project. To focus this tutorial on the essentials, you’ll integrate Celery into an existing Django app. Go ahead and download the code for that app so that you can follow along: Get Your Code: Click here to download free the sample code you’ll use to integrate Celery into your Django app.

Python Celery Basics Celery is a distributed task queue that can collect, record, schedule, and perform tasks outside of your main program. Note: Celery dropped support for Windows in version 4, so while you may still be able to get it to work on Windows, you’re better off using a different task queue, such as huey or Dramatiq , instead. In this tutorial, you’ll focus on using Celery on UNIX systems, so if you’re trying to set up a distributed task queue on Windows , then this might not be the right tutorial for you.

To receive tasks from your program and send results to a back end, Celery requires a message broker for communication. Redis and RabbitMQ are two message brokers that developers often use together with Celery. In this tutorial, you’ll use Redis as the message broker. To challenge yourself, you can stray from the instructions and use RabbitMQ as a message broker instead. If you want to keep track of the results of your task runs, then you also need to set up a results back end database.

Note: Connecting Celery to a results back end is optional. Once you instruct Celery to run a task, it’ll do its duty whether you keep track of the task result or not. However, keeping a record of all task results is often helpful, especially if you’re distributing tasks to multiple queues. To persist information about task results, you need a database back end. You can use many different databases to keep track of Celery task results.

In this tutorial, you’ll work with Redis both as a message broker and as a results back end. By using Redis, you limit the dependencies that you need to install because it can take on both roles. You won’t do any work with the recorded task results in the scope of this tutorial. However, as a next step, you could inspect the results with the Redis command-line interface (CLI) or pull information into a dedicated page in your Django project.

Remove ads Why Use Celery? There are two main reasons why most developers want to start using Celery: Offloading work from your app to distributed processes that can run independently of your app Scheduling task execution at a specific time, sometimes as recurring events Celery is an excellent choice for both of these use cases. It defines itself as “a task queue with focus on real-time processing, while also supporting task scheduling” ( Source ).

Even though both of these functionalities are part of Celery, they’re often addressed separately: Celery workers are worker processes that run tasks independently from one another and outside the context of your main service. Celery beat is a scheduler that orchestrates when to run tasks. You can use it to schedule periodic tasks as well. Celery workers are the backbone of Celery. Even if you aim to schedule recurring tasks using Celery beat, a Celery worker will pick up your instructions and handle them at the scheduled time.

What Celery beat adds to the mix is a time-based scheduler for Celery workers. In this tutorial, you’ll learn how to integrate Celery with Django to perform operations asynchronously from the main execution thread of your app using Celery workers. You won’t tackle task scheduling with Celery beat in this tutorial, but once you understand the basics of Celery tasks, you’ll be well equipped to set up periodic tasks with Celery beat.

Note: Since version 6.0, Django ships with a built-in Tasks framework that lets you mark a function with the @task decorator and enqueue it from a view. It’s a lighter-weight alternative for deferring a single job outside of the request-response cycle. However, Django only includes backends for development and testing. In production, you still need to install a third-party backend and run a separate worker process, and there’s no built-in scheduler.

If you need periodic tasks, multiple queues, or a fleet of distributed workers, then Celery remains the more complete option, and it’s what you’ll set up in this tutorial. How Can You Leverage Celery for Your Django App? Celery isn’t only useful for web applications, but it’s certainly popular in that context. That’s because you can efficiently tackle some everyday situations in web development by using a distributed task queue such as Celery: Email sending: You may want to send an email verification, a password reset email, or a confirmation of a form submission.

Sending emails can take a while and slow down your app, especially if it has many users. Image processing: You might want to resize avatar images that users upload or apply some encoding on all images that users can share on your platform. Image processing is often a resource-intensive task that can slow down your web app, mainly if you’re serving a large community of users. Text processing: If you allow users to add data to your app, then you might want to monitor their input.

For example, you may want to check for profanity in comments or translate user-submitted text to a different language. Handling all this work in the context of your web app can significantly impair performance. API calls and other web requests: If you need to make web requests to provide the service that your app offers, then you can quickly run into unexpected wait times. This is true for rate-limited API requests just as much as other tasks, such as web scraping .

It’s often better to hand off these requests to a different process. Data analysis: Crunching data is notoriously resource-intensive. If your web app analyzes data for your users, you’ll quickly see your app become unresponsive if you’re handling all the work right within Django. Machine learning model runs: Just like with other data analysis, waiting for the results of machine learning operations can take a moment.

Instead of letting your users wait for the calculations to complete, you can offload that work to Celery so they can continue browsing your web app until the results come back. Report generation: If you’re serving an app that allows users to generate reports f

The rest of this page is on the live site.