9 Web Applications

 

This chapter covers:

  • Creating web applications with aiohttp
  • The asynchronous server gateway interface (ASGI)
  • Creating ASGI web applications with Starlette
  • Using Django’s asynchronous views

Web applications power most of the sites we use on the internet today. If you’ve worked as a developer for a company with an internet presence, you’ve likely worked on a web application at some point in your career. In the world of synchronous Python, this means you’ve used frameworks such as Flask, Bottle or the extremely popular Django. With the exception of more recent versions of Django, these web frameworks were not built to work with asyncio out of the box. As such, when our web applications do work that could be parallelized, such as querying a database or making calls to other APIs, we don’t have options outside of multithreading or multiprocessing. This means that we’ll need to explore new frameworks that are compatible with asyncio.

9.1      Creating a REST API with Aiohttp

9.1.1   What is REST?

9.1.2   Aiohttp server basics

9.1.3   Connecting to a database and returning results

9.1.4   Comparing Aiohttp with Flask

9.2      The asynchronous server gateway interface

9.2.1   How does ASGI compare to WSGI?

9.3      ASGI with Starlette

9.3.1   A REST endpoint with Starlette

9.3.2   Web sockets with Starlette

9.4      Django Asynchronous Views

9.4.1   Running blocking work in an asynchronous view

9.4.2   Using async code in synchronous views

9.5      Summary