Last year, when Django 3.0 came out there was a lot of buzz in the developer community about how Django 3.0 has support for asynchronous now but what does that mean for most developers? I guess we'll find out.
While it's true that Django has developing support for asynchronous (“async”) Python, but it does not yet support asynchronous views or middleware; they will be coming sometime in a future release.
As per the Django documentation,
There is limited support for other parts of the async ecosystem; namely, Django can natively talk ASGI and some async safety support.
Alright, I get that some of you might not be knowing what 'asynchronous' really means cause maybe you've just been using Python the way it is. I'll clear that up.
What is asynchronous code?
Python is a single-threaded language unlike Java or some other multi-threaded applications translating to just running in a single sequence instead of doing multiple tasks at once.
Since Django is a web framework built on top of Python, it's not asynchronous either and that means there are issues like a view in a Django application getting stuck in a case where one or more operations take too much time to complete. This can become an implication.
If you try to simulate a blocking event in the view with the sleep set to a specific time ( sleep is from time library in Python ), you'll notice that the view gets stuck for the specific time before moving on.
We can draw from our observation that Django without queues is not really meant for I/O bound activities and that is a massive issue for some developers. Now, if you're a Python developer, you might already know how you could simply utilize the module `asyncio` to make your Python code asynchronous.
But hey, what about Django? How do I make that asynchronous?
Well, technically you can't BUT there's a sweet way around it and most developers don't seem to mind cause well, it just works. Even Instagram scaled with Django for its operations and is currently the largest deployment of Django at scale.
What's that sweet way around I was talking about earlier?
Celery. Yeah, Celery is exactly what you need.
Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operations but supports scheduling as well. So yeah you can have asynchronous like features in Django as well.
How do I get started with it?
You could either try getting started with [its] documentation which is what I would recommend since documentation are usually well-written or you could look at some of the video/text-based tutorials out there.
These are the resources:
I hope this post gave you some sort of insight on whether Django is truly asynchronous or not but either way, if you made it to the bottom by actually reading it throughout, lemme know if I could improve this post by adding something I might have missed.
Check out my previous post on Setting up your own Tor Hidden Service.
Previously published at https://dev.to/arthtyagi/can-django-be-asynchronous-493b