Django Async Support: Building High-Performance Web Apps

Django was built synchronous, and that shows the moment you need WebSockets, HTTP/2 or long-lived connections. Since version 3 it speaks ASGI — a superset of WSGI that keeps existing code working while opening the door to async views. This article shows what that changes in practice, and where it does not help.

Starting with Django version 3, you can use async core support.

For many years WSGI (Web Server Gateway Interface) has been a simple and universal interface between python applications and web servers. By design, WSGI servers are synchronous. The server blocks every single request until any I/O operation is completed. Thus solving the C10k problem can be a challenge!

Main

 

Django is a very powerful tool to create systems of any kind within a very short timeframe. Unfortunately, its synchronous nature imposes constraints on using some modern web technologies such as WebSockets or HTTP/2 features.

Here is where ASGI (Asynchronous Standard Gateway Interface) comes to our rescue. In fact, ASGI is a superset of WSGI and it is capable of calling WSGI callables. ASGI has support for websockets and long-lived HTTP connections. It can also be used for HTTP long-polling, or HTTP Server Sent Events that allow for uni-directional server-to-client updates.

To extend Django so that it can use WebSockets, Channels 2 were created. This is a new layer to handle the use of WebSockets and background tasks. Nevertheless, it is not part of the Django core, it requires some additional installation and configuration job to be done before you can use it. That’s why the out-of-the-box built-in support for async features solves the headache of Websocket integration.

Switching to async can take some time. The current release (3.0) is reported to natively support ASGI as well as some async safety features. Being a big task, it requires reimplementation of many components like ORM, request handler, Template renderer etc to work in asynchronous mode, which is expected to become available in the next releases. Once it is done, we’ll have an even more powerful and robust web framework in the async world to continue delivering the full benefit of fast and solid systems of any kind.

Stay Ahead with GRSE Insights:
Get expert articles, technology trends, industry updates, and practical software engineering insights delivered straight to your inbox.
Subscribe Now