Chapter 4. Async, Concurrency, and Starlette Tour
Starlette is a lightweight ASGI framework/toolkit, which is ideal for building async web services in Python.
Tom Christie, creator of Starlette
Preview
The previous chapter briefly introduced the first things a developer
would encounter on writing a new FastAPI application.
This chapter
emphasizes FastAPI’s underlying Starlette library,
particularly its support of async processing.
After an overview of multiple ways of
“doing more things at once” in Python,
you’ll see how its newer async and await
keywords have been incorporated into Starlette and FastAPI.
Starlette
Much of FastAPI’s web code is based on the Starlette package, which was created by Tom Christie. It can be used as a web framework in its own right or as a library for other frameworks, such as FastAPI. Like any other web framework, Starlette handles all the usual HTTP request parsing and response generation. It’s similar to Werkzeug, the package that underlies Flask.
But its most important feature is its support of the modern Python asynchronous web standard: ASGI. Until now, most Python web frameworks (like Flask and Django) have been based on the traditional synchronous WSGI standard. Because web applications so frequently connect to much slower code (e.g., database, file, and network access), ASGI avoids the blocking and busy waiting of WSGI-based applications.
As a result, Starlette and frameworks that use it are the fastest Python web packages, rivaling even ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access