pip install "uvicorn[standard]"★[standard]adds the fast extras: uvloop, httptools, websockets, and watchfiles (for reload). Plainuvicornis pure-Python.uvicorn main:app★Run the ASGI callableappinmain.py. Serves on127.0.0.1:8000by default. Works with FastAPI/Starlette/Django-ASGI.uvicorn main:app --host 0.0.0.0 --port 8000★0.0.0.0to accept external connections. This is the minimal production-ish invocation.uvicorn main:app --reload # dev auto-reload★--reloadrestarts on file changes — dev only (never in production).
uvicorn main:app --workers 4★Run 4 worker processes — Uvicorn now manages its own multiprocessing (you no longer need Gunicorn just for this). Mutually exclusive with--reload.uvicorn main:app --log-level warning --no-access-logControl logging.--log-config file.json/.yamlfor full control.uvicorn main:app --proxy-headers --forwarded-allow-ips '*'★TrustX-Forwarded-For/Protowhen behind nginx/a load balancer so the app sees the real client IP/scheme.uvicorn main:app --env-file .env --timeout-keep-alive 5Load env vars; tune keep-alive.--loop uvloop,--http httptools,--lifespan onalso available.
import uvicorn if __name__ == "__main__": uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)★Start Uvicorn from Python. ⚠️ Pass the app as an import string ("main:app"), not the object, when usingreload=True/workers=.async def app(scope, receive, send): ... # a raw ASGI appAny ASGI callable works; usually it's a framework instance (app = FastAPI()).# uvicorn.run(app_object, reload=True) -> reload won't workgotchaReload & multiple workers require the import-string form so child processes can re-import the app.