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.

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.

#django #python #developer

How to Get Started with Asynchronous Views in Django 3.1
3.25 GEEK