Skip to main content

🚀 Context Manager to run uvicorn ASGI applications

uvicorn_context_manager is a Python package that exposes a class named UvicornContextManager that inherits from uvicorn.Server and contains a Python context manager to deploy an ASGI application inside a context and then gracefully shut it down.

The motivation was to provide a way to easily deploy an ASGI application when running tests or wanting to interact temporarily with ASGI applications.

This could have been a GitHub Gist or just a code snippet, but I think that this can be useful for many uvicorn users, and it just contains the uvicorn dependency so there's no overhead.

⚒️ Installation

Python greater than 3.7 and lower than 3.11

pip install uvicorn_context_manager

Or install it in development mode as:

pip install -e .

👨🏻‍💻 Usage

⚡️ With fastapi

As mentioned before, the context manager as uvicorn, lets you deploy any ASGI application. Since fastapi is one of the most used Python frameworks for web development according to a JetBrains survey, see https://twitter.com/tiangolo/status/1624002347776065538.

So on, we think it's useful to showcase how to use uvicorn_context_manager with fastapi.

pip install fastapi

Once installed, you can deploy your ASGI application in a Python context manager as:

from fastapi import FastAPI
from uvicorn_context_manager import UvicornContextManager

api = FastAPI()
with UvicornContextManager(api) as server:
    ...

🥇 Credits

Credits go to @florimondmanca due to the solution provided at encode/uvicorn#1103, as the motivation to create this package was based on that code snippet.

Metadata

Release files for uvicorn_context_manager 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for uvicorn_context_manager 0.0.1
File Size Uploaded
uvicorn_context_manager-0.0.1.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for uvicorn_context_manager 0.0.1
File Interpreter ABI Platform
uvicorn_context_manager-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / uvicorn_context_manager-0.0.1.tar.gz

Download URL uvicorn_context_manager-0.0.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
411f3759048d2115854e1649c04bb0bfe9515dc4d079dfe1c98f35b0ce80e71d
BLAKE2b-256 checksum
How to use checksums
4b9bdbd6d9be67ddb26af3b0238f4eb46818159ad0997baf019d9468f53a8354
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.23.3

Release files / uvicorn_context_manager-0.0.1-py3-none-any.whl

Download URL uvicorn_context_manager-0.0.1-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc0460b1f75b385dfd241e24d55fd4c3d85fc6f4c61506071e26883d781a4384
BLAKE2b-256 checksum
How to use checksums
5df91f4e3ac92a7b93771b0cb01592aece0862538ec270bee9fc927af6956052
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.23.3

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page