Manually with ASGI server#

ASGI (Asynchronous Server Gateway Interface) is intended to provide a standard interface between async Python web frameworks like Litestar, and async web servers.

There are several popular ASGI servers available, and you can choose the one that best fits your application’s needs.

Use When#

Running your application manually with an ASGI server is usually only ideal in development and testing environments.

It is generally recommended to run your production workloads inside a containerized environment, such as Docker or Kubernetes or via a process control system such as Supervisor or systemd.

Alternatives#

For different deployment scenarios, consider these alternatives:

  • NGINX Unit:

    A dynamic web and application server, suitable for running and managing multiple applications.

  • systemd:

    A system and service manager, integrated into many Linux distributions for managing system processes.

    Note

    Official documentation coming soon

  • Supervisor:

    A process control system that can be used to automatically start, stop and restart processes; includes a web UI.

  • Docker:

    Ideal for containerized environments, offering isolation and scalability.

Choosing an ASGI Server#

Uvicorn is an ASGI server that supports HTTP/1.1 and WebSocket.

Hypercorn is an ASGI server that was initially part of Quart, and supports HTTP/1.1, HTTP/2, and WebSocket.

Daphne is an ASGI server that was originally developed for Django Channels, and supports HTTP/1.1, HTTP/2, and WebSocket.

Granian is a Rust-based ASGI server that supports HTTP/1.1, HTTP/2, and WebSocket.

Install the ASGI Server#

Install Uvicorn with pip#
pip install uvicorn
Install Hypercorn with pip#
pip install hypercorn
Install Daphne with pip#
pip install daphne
Install Granian with pip#
pip install granian

Run the ASGI Server#

Assuming your app is defined in the same manner as Minimal Example, you can run the ASGI server with the following command:

Run Uvicorn with the default configuration#
uvicorn app:app
Console Output#
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Run Hypercorn with the default configuration#
hypercorn app:app
Console Output#
[2023-11-12 23:31:26 -0800] [16748] [INFO] Running on http://127.0.0.1:8000 (CTRL + C to quit)
Run Daphne with the default configuration#
daphne app:app
Console Output#
INFO - 2023-11-12 23:31:51,571 - daphne.cli - cli - Starting server at tcp:port=8000:interface=127.0.0.1
INFO - 2023-11-12 23:31:51,572 - daphne.server - server - Listening on TCP address 127.0.0.1:8000
Run Granian with the default configuration#
granian --interface asgi app:app
Console Output#
[INFO] Starting granian
[INFO] Listening at: 127.0.0.1:8000