DEV Community

Cover image for Can Django be asynchronous?

Posted on

Can Django be asynchronous?

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:

  1. Celery Documentation
  2. Real Python Tutorial
  3. Pretty Printed

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 PipEnv Vs Venv.

Top comments (4)

gwutama profile image
Galuh Utama • Edited

Good article. I haven’t got the time to try my hands on Django 3.0.

Python has support for true multithreading, though. Just like you said, with asyncio and also threading modules. OK there’s limitations with green threads and so on. Applications written in Java won’t automatically be multithreaded unless you spawn threads within the process. JavaScript and nodejs in general, AFAIK, is on the other hand don’t have support for multithreading.

ale_jacques profile image
Alexandre Jacques

Just for clarificarion: Django 3.0 (and soon 3.1) Django have and will have more support for async:

And, for the Celery part, it is great. The caveat is that its hard to configure it right. There are alternatives, though.

Django RQ:

They are simpler (not as powerful, of course) but depending on the async task(s) trying to be achieved, they very well can suit.

arthtyagi profile image

Yeah most definitely, I had a look at Django Q with Redis but I just didn't feel that it was as versatile as Celery hence I just use Celery for myself.

paulwababu profile image

Celery or Django channels is an overkill though;)