Async Views in Django 3.1

Last updated August 17th, 2020

Writing asynchronous code gives you the ability to speed up your application with little effort. With Django 3.1 finally supporting async views, middleware, and tests, now's a great time to get them under your belt.

This post looks at how to get started with Django's new 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 post.

Contents

Objectives

By the end of this post, you should be able to:

  1. Write an asynchronous view in Django
  2. Make a non-blocking HTTP request in a Django view
  3. Simplify basic background tasks with Django's async views
  4. Use sync_to_async to make a synchronous call inside an async view
  5. Explain when you should and shouldn't use async views

You should also be able to answer the following questions:

  1. What if you make a synchronous call inside an async view?
  2. What if you make a synchronous and an asynchronous call inside an async view?
  3. Is Celery still necessary with Django's async views?

Prerequisites

As long as you're already familiar with Django itself, adding asynchronous functionality to non-class-based views is extremely straightforward.

Dependencies

  1. Python >= 3.8
  2. Django >= 3.1
  3. Uvicorn
  4. HTTPX

What is ASGI?

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.

Creating the App

Create a new project directory along with a new Django project:

$ mkdir django-async-views && cd django-async-views
$ python3.8 -m venv env
$ source env/bin/activate

(env)$ pip install django
(env)$ django-admin.py startproject hello_async .

Feel free to swap out virtualenv and Pip for Poetry or Pipenv.

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 use Uvicorn to stand up the server.

Install it:

(env)$ pip install uvicorn

To run your project with Uvicorn, you use the following command from your project's root:

uvicorn {name of your project}.asgi:application

In our case, this would be:

(env)$ uvicorn hello_async.asgi:application

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

from django.http import HttpResponse


async def index(request):
    return HttpResponse("Hello, async Django!")

Creating async views in Django is as simple as creating a synchronous view -- all you need to do is add the async keyword.

Update the URLs:

# hello_async/urls.py

from django.contrib import admin
from django.urls import path

from hello_async.views import index


urlpatterns = [
    path("admin/", admin.site.urls),
    path("", index),
]

Now, in a terminal in your root folder, run:

(env)$ uvicorn hello_async.asgi:application --reload

The --reload flag tells uvicorn to watch your files for changes and reload if it finds any. That was probably self-explanatory.

Open http://localhost:8000/ in your favorite web browser:

Hello, async Django!

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.

To demonstrate this, add a few new views:

# hello_async/views.py

import asyncio
from time import sleep

import httpx
from django.http import HttpResponse


# helpers

async def http_call_async():
    for num in range(1, 6):
        await asyncio.sleep(1)
        print(num)
    async with httpx.AsyncClient() as client:
        r = await client.get("https://httpbin.org/")
        print(r)


def http_call_sync():
    for num in range(1, 6):
        sleep(1)
        print(num)
    r = httpx.get("https://httpbin.org/")
    print(r)


# views

async def index(request):
    return HttpResponse("Hello, async Django!")


async def async_view(request):
    loop = asyncio.get_event_loop()
    loop.create_task(http_call_async())
    return HttpResponse("Non-blocking HTTP request")


def sync_view(request):
    http_call_sync()
    return HttpResponse("Blocking HTTP request")

Update the URLs:

# hello_async/urls.py

from django.contrib import admin
from django.urls import path

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:

(env)$ pip install httpx

With the server running, navigate to http://localhost:8000/async/. You should immediately see the response:

Non-blocking HTTP request

In your terminal you should see:

INFO:     127.0.0.1:60374 - "GET /async/ HTTP/1.1" 200 OK
1
2
3
4
5
<Response [200 OK]>

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:

Blocking HTTP request

Turn to the terminal:

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.

Smoking Some Meats

Now, let's write a view that runs a simple task in the background.

Back in your project's URLconf, create a new path at smoke_some_meats:

# hello_async/urls.py

from django.contrib import admin
from django.urls import path

from hello_async.views import index, async_view, sync_view, smoke_some_meats


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 function called smoke. This function takes two parameters: a list of strings called smokables and a string called flavor. These will default to a list of smokable meats and "Sweet Baby Ray's", respectively.

# hello_async/views.py

async def smoke(smokables: List[str] = None, flavor: str = "Sweet Baby Ray's") -> None:
    """ Smokes some meats and applies the Sweet Baby Ray's """

    if smokables is None:
        smokables = [
            "ribs",
            "brisket",
            "lemon chicken",
            "salmon",
            "bison sirloin",
            "sausage",
        ]

    if (loved_smokable := smokables[0]) == "ribs":
        loved_smokable = "meats"

    for smokable in smokables:
        print(f"Smoking some {smokable}....")
        await asyncio.sleep(1)
        print(f"Applying the {flavor}....")
        await asyncio.sleep(1)
        print(f"{smokable.capitalize()} smoked.")

    print(f"Who doesn't love smoked {loved_smokable}?")

The first line of the function instantiates the default list of meats if smokables isn't provided. The second "if" statement then sets a variable called loved_smokable to the first object in smokables, so long as the first object isn't "ribs." The for loop asynchronously applies the flavor (read: Sweet Baby Ray's) to the smokables (read: smoked meats).

Don't forget the import:

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).

Next, create the async view that uses the async smoke function:

# hello_async/views.py

async def smoke_some_meats(request) -> HttpResponse:
    loop = asyncio.get_event_loop()
    smoke_args = []

    if to_smoke := request.GET.get("to_smoke"):
        # Grab smokables
        to_smoke = to_smoke.split(",")
        smoke_args += [[smokable.lower().strip() for smokable in to_smoke]]

        # Do some string prettification
        if (smoke_list_len := len(to_smoke)) == 2:
            to_smoke = " and ".join(to_smoke)
        elif smoke_list_len > 2:
            to_smoke[-1] = f"and {to_smoke[-1]}"
            to_smoke = ", ".join(to_smoke)

    else:
        to_smoke = "meats"

    if flavor := request.GET.get("flavor"):
        smoke_args.append(flavor)

    loop.create_task(smoke(*smoke_args))

    return HttpResponse(f"Smoking some {to_smoke}....")

This view takes the optional query params to_smoke and flavor. to_smoke is a comma-delimited list of things to smoke, whereas flavor is what you're applying to them.

The first thing this view does (which can't be done in a standard sync view) is grab the event loop with asyncio.get_event_loop(). It then parses the query params, if applicable (and does some string cleanup for the final print statement). If we don't pass in anything to smoke, to_smoke defaults to "meats." Finally, a response is returned to let the user know they're being prepared a delicious BBQ meal.

Great. Save the file, then head back to your browser and navigate to http://localhost:8000/smoke_some_meats/. You should be greeted with the response:

Smoking some meats....

In your console, you should see:

Smoking some ribs....
INFO:     127.0.0.1:56239 - "GET /smoke_some_meats/ HTTP/1.1" 200 OK

Applying the Sweet Baby Ray's....
Ribs smoked.
Smoking some brisket....
Applying the Sweet Baby Ray's....
Brisket smoked.
Smoking some lemon chicken....
Applying the Sweet Baby Ray's....
Lemon chicken smoked.
Smoking some salmon....
Applying the Sweet Baby Ray's....
Salmon smoked.
Smoking some bison sirloin....
Applying the Sweet Baby Ray's....
Bison sirloin smoked.
Smoking some sausage....
Applying the Sweet Baby Ray's....
Sausage smoked.
Who doesn't love smoked meats?

Notice how the ribs started smoking before the 200 response was logged. This is asynchronicity at work: As the smoke function initially sleeps for one second, the view finishes up processing and returns the response. The end user will see the response just as the meats start smoking.

It's also worth noting that if you use Django's dev server, your server will return a response just fine, but anything asynchronous just won't happen. Here's how your console's log would appear:

Smoking some ribs....
[16/Aug/2020 22:37:03] "GET /smoke_some_meats/ HTTP/1.1" 200 22

Using Uvicorn, we can also test out using query params. Try http://localhost:8000/smoke_some_meats?to_smoke=ice cream, bananas, cheese&flavor=Gold Bond Medicated Powder. (spaces will be conveniently converted automatically)

Browser:

Smoking some ice cream, bananas, and cheese....

Terminal:

Smoking some ice cream....
INFO:     127.0.0.1:56407 - "GET /smoke_some_meats/?to_smoke=ice%20cream,%20bananas,%20cheese&flavor=Gold%20Bond%20Medicated%20Powder HTTP/1.1" 200 OK
Applying the Gold Bond Medicated Powder....
Ice cream smoked.
Smoking some bananas....
Applying the Gold Bond Medicated Powder....
Bananas smoked.
Smoking some cheese....
Applying the Gold Bond Medicated Powder....
Cheese smoked.
Who doesn't love smoked ice cream?

Burnt Meats

Sync Call

Q: What if you make a synchronous call inside an async view?

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

def oversmoke() -> None:
    """ If it's not dry, it must be uncooked """
    sleep(5)
    print("Who doesn't love burnt meats?")

Very straightforward: We're just synchronously waiting for five seconds.

Create the view that calls this function:

# hello_async/views.py

async def burn_some_meats(request):
    oversmoke()
    return HttpResponse(f"Burned some meats.")

Lastly, wire up the route in your project's URLconf:

# hello_async/urls.py

from django.contrib import admin
from django.urls import path

from hello_async.views import index, async_view, sync_view, smoke_some_meats, burn_some_meats


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),
]

Visit the route in the browser at http://localhost:8000/burn_some_meats:

Burned some meats.

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:

Who doesn't love burnt meats?
INFO:     127.0.0.1:40682 - "GET /burn_some_meats HTTP/1.1" 200 OK

It's possibly worth noting that the same thing will happen regardless of the server you're using, be it WSGI or ASGI-based.

Sync and Async Calls

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:

# hello_async/views.py

async def async_with_sync_view(request):
    loop = asyncio.get_event_loop()
    async_function = sync_to_async(http_call_sync)
    loop.create_task(async_function())
    return HttpResponse("Non-blocking HTTP request (via sync_to_async)")

Add the import to the top:

from asgiref.sync import sync_to_async

Add the URL:

# hello_async/urls.py

from django.contrib import admin
from django.urls import path

from hello_async.views import (
    index,
    async_view,
    sync_view,
    smoke_some_meats,
    burn_some_meats,
    async_with_sync_view
)


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),
]

Test it out in your browser at http://localhost:8003/sync_to_async/.

In your terminal you should see:

INFO:     127.0.0.1:61365 - "GET /sync_to_async/ HTTP/1.1" 200 OK
1
2
3
4
5
<Response [200 OK]>

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.

Celery and Async Views

Q: Is Celery still necessary with Django's async views?

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 (such as send an email to a new subscriber or call an external API), 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, 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 synchronous 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):

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 new asynchronous views open up. Some other things to try in your async views are sending emails, calling third-party APIs, and writing to a file. Think of those views in your code that have simple processes in them that don't necessarily need to return anything directly to the end user -- those can quickly be converted to async.

For more on Django's newfound asynchronicity, see this excellent post that covers the same topic as well as multithreading and testing.

Jace Medlin

Jace Medlin

Jace is a US-based software engineer with interests in Python, Vue, and Rust. When he's not building web apps across the stack, he can be found recording music, collecting VHS tapes, philosophizing about the universe, and experimenting with robotics.

Share this tutorial

Featured Course

Test-Driven Development with Django, Django REST Framework, and Docker

In this course, you'll learn how to set up a development environment with Docker in order to build and deploy a RESTful API powered by Python, Django, and Django REST Framework.

Featured Course

Test-Driven Development with Django, Django REST Framework, and Docker

In this course, you'll learn how to set up a development environment with Docker in order to build and deploy a RESTful API powered by Python, Django, and Django REST Framework.