Why is ASGI considered better than WSGI for web applications that frequently access databases, files, or networks?
ASGI is better than WSGI for apps that often access databases, files, or networks because those operations are much slower than CPU work. By supporting async code, ASGI avoids the blocking and busy waiting that traditional synchronous WSGI-based frameworks suffer from, letting the server handle other requests during I/O waits.
Traditional Python web frameworks like Flask and Django are based on WSGI, a synchronous standard. Web applications frequently call much slower resources such as databases, files, and network services, and under WSGI those calls block the server while it waits. ASGI, the asynchronous standard used by Starlette and FastAPI, avoids that blocking and busy waiting. The event loop can pause an operation that is waiting on I/O and move on to handle other requests, then return when the I/O finishes. This is why async support is a major reason FastAPI is considered very fast for Python web applications.
Key points
- WSGI is the older synchronous standard used by frameworks like Flask and Django.
- Database, file, and network access are far slower than CPU calculations.
- ASGI avoids the blocking and busy waiting that WSGI-based applications experience during these slow I/O operations.
- With async code, a web server can handle other requests while one request waits for I/O.
- FastAPI inherits ASGI support through Starlette, contributing to its speed.
Related questions
FastAPI: Modern Python Web Development
Bill Lubanovic;
First Edition · O'Reilly Media, Inc.