Integrate Celery With Django

00:00 Integrate Celery with Django. Now that you know what Celery is and how it can help you improve your web app’s performance, it’s time to integrate it so you can run asynchronous tasks with Celery.

00:13 You’ll focus on integrating Celery into an existing Django project. You’ll start with a stripped-down Django app with a minimal use case: collecting user feedback and delivering an email.

00:23 As a reply, start by downloading the source code of the provided feedback app. Unzip the downloaded file and use the terminal to navigate into the source code directory where you should see a standard Django project folder structure.

00:41 As is good practice, you’ll need to create and activate a virtual environment as seen on screen.

00:54 Once a virtual environment is active, you can then install Django pinned here to version 5.0.1.

01:09 Confirm that you are inside of the source code folder, and then finish the local setup for the Django app by running the migrations

01:23 and starting the development server.

01:31 You can now open up your browser to navigate to the app’s homepage at the address seen on screen where a friendly-looking feedback form should greet you.

01:41 However, that feedback form currently only looks friendly. Go ahead, fill out the form and submit some feedback.

01:50 Imagine that one of your web app’s users would run into a situation as seen after you press the submit button, the app freezes. You can see this in the browser tab, but the page is unresponsive and you can still see all the information that you entered into the form.

02:07 It takes much too long for Django to process the form and redirect you to the success page. Django freezes because it needs to synchronously process the email sending request before tackling the next task, which is to redirect the user to the success page.

02:24 The reason it freezes for so long is because of a time.sleep() call in send_email() that simulates a time or work-intensive task that could be associated with email sending.

02:36 Of course, in an actual application, you wouldn’t add in an extra time delay to your code by making Django sleep. But whatever email service you do use will unfortunately introduce some delay for you, particularly once the app starts serving multiple users, you’ll run into limitations.

02:53 You can substitute the time.sleep() call with whatever work-intensive process you need to perform in your web app to serve your users. The Django application shouldn’t handle long-running tasks synchronously because doing so impairs your app’s user experience and overall usefulness.

03:09 Instead, you’ll learn how to hand off this task to a Celery worker. Celery workers tackle computations as a background task and allow your users to continue browsing your web app without delay.

03:23 In the next section of the course, you’ll start this process by looking at how to install Celery as your task queue.

Become a Member to join the conversation.