Ephes Blog

Miscellaneous things. Not sure what to put here, yet.


Django 3.1 Async

- Jochen

With version 3.1, you can finally use asynchronous views, middlewares and tests in Django. Support for async database queries will follow later. You don’t have to change anything if you don’t want to use those new async features . All of your existing synchronous code will run without modification in Django 3.1.

Async support for Django is on it’s way for quite some time now. Since version 3.0 there’s support for ASGI included. But there was not much benefit for end users though. The only thing you could do concurrently were file uploads, since uploads don’t reach the view layer which was not async capable in Django 3.0.

When do you might want to use those new features? If you are building applications that have to deal with a high number of tasks simultaneously. Here are some examples:

  • Chat services like Slack
  • Gateway APIs / Proxy Services
  • Games, especially MMOs like Eve Online
  • Applications using Phoenix Liveview - check out Phoenix Phrenzy results for additional examples
  • A reactive version of Django Admin where model changes are shown interactively
  • A new api frontend for Django REST framework updating list endpoints interactively as new data comes in
  • All kinds of dashboard applications showing currently active connections, requests per second updating in realtime

As Tom Christie explained in his talk Sketching out a Django redesign held at DjangoCon 2019 the core question is this: Do we want to have to switch languages to support those use cases? And while his Starlette project (gaining popularity recently in combination with the FastAPI framework) is allowing us to do all this in Python, we also might want to keep using Django.

What to Expect from this Article?

  1. A small example on how to use async views, middlewares and tests
  2. Why is async such a big deal anyway?
  3. The gory details of multithreading vs async, GIL and other oddities

Estimated read time: 25 minutes
There's also a podcast episode elaborating a little bit more on this topic (it's in german).