coro-context-manager
coro-context-manager is a simple python package that includes an object that can wrap a coroutine to allow it to behave as a context manager or a regular awaitable.
This class is super useful when you have a coroutine that returns an object that defines an async context manager using
__aenter__ and __aexit__
Installation
pip
pip install coro-context-manager
poetry
poetry add coro-context-manager
Usage
CoroContextManager can be used to wrap a coroutine so that it can be awaited or called via an async context manager
in which case the library will try to use the underlying object's __aenter__ and __aexit__, if they exist.
import asyncio
from coro_context_manager import CoroContextManager
class MyObject:
def __init__(self, initial_value):
self.some_value = initial_value
async def __aenter__(self):
await asyncio.sleep(.1)
self.some_value += 1
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await asyncio.sleep(.1)
self.some_value -= 1
@classmethod
async def an_io_intensive_constructor(cls, initial_value):
await asyncio.sleep(10)
return cls(initial_value)
async def main():
"""
Using CoroContextManager, I get a coroutine I can await or use with an async context manager, which proxies to
the context manager defined on object returned by the coroutine, if it exists.
"""
# i can await it directly
myobj = await CoroContextManager(MyObject.an_io_intensive_constructor(5))
print(type(myobj))
# <class '__main__.MyObject'>
# or use it as an async context manager, not having to await it, with the same api!
async with CoroContextManager(MyObject.an_io_intensive_constructor(5)) as myobj:
print(type(myobj))
# <class '__main__.MyObject'>
print(myobj.some_value)
# 6
print(myobj.some_value)
# 5
asyncio.run(main())
Rationale
This is a common enough pattern used in several async packages all with slightly different implementation. It would be nice if there was a consistent pattern everyone was using; this package aims to provide that.
Metadata
Release files for coro-context-manager 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| coro_context_manager-0.2.1.tar.gz | 4.1 kB | Without provenance | Details for coro_context_manager-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| coro_context_manager-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for coro_context_manager-0.2.1-py3-none-any.whl |
Total release size: 8.4 kB
Release files / coro_context_manager-0.2.1.tar.gz
| Download URL | coro_context_manager-0.2.1.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3bf30bd52016cc22f5200d65fb306eb96fd040e82e0fbf8d820f85cce74b81f1
|
|
BLAKE2b-256 checksum How to use checksums |
1d3f9be07bcaae33f7647349c555bff21776cff852b4b81459897818b9d5b3ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.9.21 Linux/6.8.0-1017-azure
|
Release files / coro_context_manager-0.2.1-py3-none-any.whl
| Download URL | coro_context_manager-0.2.1-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8bf4527dbeab7ff96ef7d92326ce43b1755685e35b991090f18c82520d0e3287
|
|
BLAKE2b-256 checksum How to use checksums |
98e8be0f049c12854197b4ef401436a7a05cfd35a7165065dd463f4a6fd8ec2f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.9.21 Linux/6.8.0-1017-azure
|
Security for coro-context-manager 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.