How to create an asynchronous task in Celery with Django?
First, solve a problem, then again solve 100 more problems.
3 min read
This is the third part of this blog series where we are learning to use
Celery in a Django project, check out the full series here.
Up until now, we have seen how to integrate and setup
Celery in Django, in this blog we are going to create our first asynchronous task using
Before we move to further steps, make sure that you have followed all the steps given in Blog 1 & Blog 2 of this series.
Creating first Asynchronous task:
Creating an asynchronous task in
Celery is pretty easy, and somewhat similar to how we create
models in Django.
We are going to have a
tasks.py file inside our Django app, and then we will write our asynchronous function inside it.
*Note: Few things I am assuming here is that, our Django project name is
myproject, you have to use your project name instead.*
Let’s write our first asynchronous task in a Django app named
core, we will create a simple addition function which will add two numbers but asynchronously:
from celery import shared_task def add(x, y): return x + y
Looks simple, right? Well yes, it is that simple to write an asynchronous task which will execute without affecting/blocking your Django
But how can we execute it inside our view? Well, let’s do that.
Now we just need to open our
view.py file and run the above
# A Simple Django View which return an HTTP Response from django.http import HttpResponse from .tasks import add def addition_view(request): add.apply_async(args=(2,5)) # we are passing 2 & 3 in our add function return "Done the addition"
That is how we execute a
Celery task inside our Django
But this looks pretty normal, let’s add some heavy lifting in our
tasks.py which will intentionally take some time to execute, and we’ll test whether it is stopping our Django
view or not.
Let’s edit our
import time from celery import shared_task def add(x, y): print('Started addition') time.sleep(5) # Pause the execution for 5 seconds print('Done addition') return x + y
We have used
time module to pause the
function execution for 5 seconds.
Now if you will try to run your
addition_view, it will run normal as it was running before without holding for 5 seconds. Because behind the scenes, it has transferred the burden of running the
add function to
Celery instance and
Hope now you have got the clear picture of how
Celery works. Do not forget to ask your doubts in the comments section.
That is it for this blog, and the end of this blog series on
Celery, hope you have got to learn something from this.
For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.
- LinkedIn: https://www.linkedin.com/in/mnamegaurav/
- YouTube: https://www.youtube.com/c/devjunction
- Website: https://gaurav.devjunction.in/
- GitHub: https://github.com/mnamegaurav
- Instagram: https://www.instagram.com/mnamegaurav/
- Twitter: https://twitter.com/mnamegaurav
Did you find this article valuable?
Support Dev.Junction by becoming a sponsor. Any amount is appreciated!