Async Views in Django
Async Views in Django
io /blog/django-async-views/
Writing asynchronous code gives you the ability to speed up your application with little effort. Django
versions >= 3.1 support async views, middleware, and tests. If you haven't already experimented with
async views, now's a great time to get them under your belt.
This tutorial looks at how to get started with Django's asynchronous views.
If you're interested in learning more about the power behind asynchronous code along with
the differences between threads, multiprocessing, and async in Python, check out my
Speeding Up Python with Concurrency, Parallelism, and asyncio article.
Objectives
By the end of this tutorial, you should be able to:
Prerequisites
As long as you're already familiar with Django itself, adding asynchronous functionality to non-class-
based views is extremely straightforward.
Dependencies
What is ASGI?
1/13
ASGI stands for Asynchronous Server Gateway Interface. It's the modern, asynchronous follow-up to
WSGI, providing a standard for creating asynchronous Python-based web apps.
Another thing worth mentioning is that ASGI is backwards-compatible with WSGI, making it a good
excuse to switch from a WSGI server like Gunicorn or uWSGI to an ASGI server like Uvicorn or Daphne
even if you're not ready to switch to writing asynchronous apps.
Feel free to swap out virtualenv and Pip for Poetry or Pipenv. For more, review Modern
Python Environments.
Django will run your async views if you're using the built-in development server, but it won't actually run
them asynchronously, so we'll run Django with Uvicorn.
Install it:
To run your project with Uvicorn, you use the following command from your project's root:
Next, let's create our first async view. Add a new file to hold your views in the "hello_async" folder, and
then add the following view:
# hello_async/views.py
2/13
Creating async views in Django is as simple as creating a synchronous view -- all you need to do is add
the async keyword.
# hello_async/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("", index),
]
The --reload flag tells Uvicorn to watch your files for changes and reload if it finds any.
That was probably self-explanatory.
Not the most exciting thing in the world, but, hey, it's a start. It's worth noting that running this view with a
Django's built-in development server will result in exactly the same functionality and output. This is
because we're not actually doing anything asynchronous in the handler.
HTTPX
It's worth noting that async support is entirely backwards-compatible, so you can mix async and sync
views, middleware, and tests. Django will execute each in the proper execution context.
# hello_async/views.py
import asyncio
from time import sleep
import httpx
3/13
from django.http import HttpResponse
# helpers
def http_call_sync():
for num in range(1, 6):
sleep(1)
print(num)
r = httpx.get("https://httpbin.org/")
print(r)
# views
def sync_view(request):
http_call_sync()
return HttpResponse("Blocking HTTP request")
# hello_async/urls.py
4/13
from hello_async.views import index, async_view, sync_view
urlpatterns = [
path("admin/", admin.site.urls),
path("async/", async_view),
path("sync/", sync_view),
path("", index),
]
Install HTTPX:
With the server running, navigate to http://localhost:8000/async/. You should immediately see the
response:
Here, the HTTP response is sent back before the first sleep call.
Next, navigate to http://localhost:8000/sync/. It should take about five seconds to get the response:
1
2
3
4
5
<Response [200 OK]>
INFO: 127.0.0.1:60375 - "GET /sync/ HTTP/1.1" 200 OK
Here, the HTTP response is sent after the loop and the request to https://httpbin.org/ completes.
5/13
Smoking Some Meats
To simulate more of a real-world scenario of how you'd leverage async, let's look at how to run multiple
operations asynchronously, aggregate the results, and return them back to the caller.
# hello_async/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("smoke_some_meats/", smoke_some_meats),
path("async/", async_view),
path("sync/", sync_view),
path("", index),
]
Back in your views, create a new async helper function called smoke. This function takes two
parameters: a list of strings called smokables and a string called flavor. These default to a list of
smokable meats and "Sweet Baby Ray's", respectively.
# hello_async/views.py
return len(smokables)
The for loop asynchronously applies the flavor (read: Sweet Baby Ray's) to the smokables (read: smoked
meats).
6/13
from typing import List
List is used for extra typing capabilities. This is not required and may be easily omitted (just
nix the : List[str] following the "smokables" parameter declaration).
await asyncio.sleep(2)
async with httpx.AsyncClient() as client:
await client.get("https://httpbin.org/")
print("Returning smokeable")
return [
"ribs",
"brisket",
"lemon chicken",
"salmon",
"bison sirloin",
"sausage",
]
await asyncio.sleep(1)
async with httpx.AsyncClient() as client:
await client.get("https://httpbin.org/")
print("Returning flavor")
return random.choice(
[
"Sweet Baby Ray's",
"Stubb's Original",
"Famous Dave's",
]
)
import random
7/13
Create the async view that uses the async functions:
# hello_async/views.py
This view calls the get_smokables and get_flavor functions concurrently. Since smoke is
dependent on the results from both get_smokables and get_flavor, we used gather to wait for
each async task to complete.
Keep in mind, that in a regular sync view, get_smokables and get_flavor would be handled
one at a time. Also, the async view will yield the execution and allow other requests to be
processed while the asynchronous tasks are processed, which allows more requests to be
handled by the same process in a particular amount of time.
Finally, a response is returned to let the user know they're delicious BBQ meal is ready.
Great. Save the file, then head back to your browser and navigate to
http://localhost:8000/smoke_some_meats/. It should take a few seconds to get the response:
Getting smokeables...
Getting flavor...
Returning flavor
Returning smokeable
8/13
Bison sirloin smoked.
Smoking some sausage...
Applying the Stubb's Original...
Sausage smoked.
INFO: 127.0.0.1:57501 - "GET /smoke_some_meats/ HTTP/1.1" 200 OK
Getting smokeables...
Getting flavor...
Returning flavor
Returning smokeable
This is asynchronicity at work: As the get_smokables function sleeps, the get_flavor function
finishes up processing.
Burnt Meats
Sync Call
The same thing that would happen if you called a non-async function from a non-async view.
--
To illustrate this, create a new helper function in your views.py called oversmoke:
# hello_async/views.py
# hello_async/views.py
9/13
# hello_async/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("smoke_some_meats/", smoke_some_meats),
path("burn_some_meats/", burn_some_meats),
path("async/", async_view),
path("sync/", sync_view),
path("", index),
]
Notice how it took five seconds to finally get a response back from the browser. You also should have
received the console output at the same time:
It's possibly worth noting that the same thing will happen regardless of the server you're using, be it
WSGI or ASGI-based.
Q: What if you make a synchronous and an asynchronous call inside an async view?
Don't do this.
Synchronous and asynchronous views tend to work best for different purposes. If you have blocking
functionality in an async view, at best it's going to be no better than just using a synchronous view.
Sync to Async
If you need to make a synchronous call inside an async view (like to interact with the database via the
Django ORM, for example), use sync_to_async either as a wrapper or a decorator.
Example:
10/13
# hello_async/views.py
Did you notice that we set the thread_sensitive parameter to False? This means that
the synchronous function, http_call_sync, will be run in a new thread. Review the docs
for more info.
# hello_async/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("smoke_some_meats/", smoke_some_meats),
path("burn_some_meats/", burn_some_meats),
path("sync_to_async/", async_with_sync_view),
path("async/", async_view),
path("sync/", sync_view),
path("", index),
]
11/13
In your terminal you should see:
Using sync_to_async, the blocking synchronous call was processed in a background thread, allowing
the HTTP response to be sent back before the first sleep call.
It depends.
Django's async views offer similar functionality to a task or message queue without the complexity. If
you're using (or are considering) Django and want to do something simple (and don't care about
reliability), async views are a great way to accomplish this quickly and easily. If you need to perform
much-heavier, long-running background processes, you'll still want to use Celery or RQ.
It should be noted that to use async views effectively, you should only have async calls in the view. Task
queues, on the other hand, use workers on separate processes, and are therefore capable of running
synchronous calls in the background, on multiple servers.
By the way, by no means must you choose between async views and a message queue -- you can easily
use them in tandem. For example: You could use an async view to send an email or make a one-off
database modification, but have Celery clean out your database at a scheduled time every night or
generate and send customer reports.
When to Use
For greenfield projects, if async is your thing, leverage async views and write your I/O processes in an
async way as much as possible. That said, if most of your views just need to make calls to a database
and do some basic processing before returning the data, you're not going to see much of an increase (if
any) over just sticking with sync views.
For brownfield projects, if you have little to no I/O processes stick with sync views. If you do have a
number of I/O processes, gage how easy it will be to rewrite them in an async way. Rewriting sync I/O to
async is not easy, so you'll probably want to optimize your sync I/O and views before trying to rewrite to
async. Plus, it's never a good idea to mix sync processes with your async views.
In production, be sure to use Gunicorn to manage Uvicorn in order to take advantage of both concurrency
(via Uvicorn) and parallelism (via Gunicorn workers):
12/13
gunicorn -w 3 -k uvicorn.workers.UvicornWorker hello_async.asgi:application
Conclusion
In conclusion, although this was a simple use-case, it should give you a rough idea of the possibilities
that Django's asynchronous views open up. Some other things to try in your async views are sending
emails, calling third-party APIs, and reading from/writing to files.
For more on Django's newfound asynchronicity, see this excellent article that covers the same topic as
well as multithreading and testing.
13/13