Python Web Development: Choosing WSGI or ASGI for a Service

Trace a request through a synchronous or asynchronous Python service and test a FastAPI route that waits on two upstream operations.

Consider a dashboard that fetches inventory and shipping estimates from different services. The browser needs both answers. While those services work, the web process mostly waits. Now consider a route that resizes an image using CPU time. Giving both routes an async signature has different consequences.

From WSGI to ASGI

WSGI defines a callable that receives the request environment and returns an iterable response. ASGI passes a connection scope plus asynchronous receive and send callables. Its HTTP and WebSocket specifications cover long-lived connections as well as ordinary requests. A conventional synchronous application can be a good fit for short request/response work. Pick an interface based on what the application must handle. WSGI specification · ASGI specification

FastAPI in enterprise environments

For the dashboard, two independent network requests can overlap if both clients yield while waiting. Here is a local example with deterministic stand-ins for those services. We used FastAPI 0.165.0, Starlette 1.10.0 and httpx2 2.16.2 in a Python 3.12 environment. The test makes a request against the in-process application and checks that both results survive. Its ten-millisecond sleeps are illustrative and carry no throughput claim.

Python
import asyncio
from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI()

async def inventory():
    await asyncio.sleep(0.01)
    return 12

async def shipping():
    await asyncio.sleep(0.01)
    return 3

@app.get("/availability")
async def availability():
    stock, days = await asyncio.gather(inventory(), shipping())
    return {"stock": stock, "shipping_days": days}

with TestClient(app) as client:
    response = client.get("/availability")
    assert response.status_code == 200
    assert response.json() == {"stock": 12, "shipping_days": 3}

A real HTTP client needs explicit timeouts, connection limits and a decision about partial results. If shipping is down, can you still show stock? Turn that answer into a second test before you add retries. FastAPI on concurrency · FastAPI testing

AI-first web architecture

A model served over the network is another upstream dependency. Set a deadline and distinguish a timed-out request from a completed prediction. A CPU-heavy model running directly inside an async request handler can occupy the event loop. Measure event-loop delay and investigate a separate process or worker when you need isolation. If clients must reconnect after a long job, save its result behind a job ID with a documented lifetime.

Performance improvements in production

Compare the same route, payload, database queries, cache state, worker count and resource limits. Record client concurrency, completed requests, failures and p95 response time. Count timeouts in the report. If the async implementation serves more requests but the database saturates sooner, measure upstream load before increasing the web worker limit. A framework change on its own cannot establish a percentage cost saving.

A migration can start with one route in a separate deployment. Keep the existing response contract and compare results at the same load. Django users should check whether synchronous middleware or a blocking database call limits the async path. A project that needs WebSockets can test disconnect and reconnect explicitly. Django async support · ASGI HTTP and WebSocket events

Correction, November 25, 2026. This revision replaces untraceable enterprise adoption, speed and cost figures and incomplete service snippets with a runnable two-dependency example and a measurement procedure.

Share this article

X Facebook LinkedIn Reddit Telegram

Your notes on this article

Your name or initials, note and date stay in this browser profile. Anyone using the profile can read them. Delete a note below or clear this site’s browser data. To reach the editors, report a correction.

No notes saved here