Small AnyIO utility for ensuring some task runs before the current async context finishes.
Project description
anyio-atexit
A small AnyIO utility for ensuring some tasks run before the current async context finishes.
This doesn't provide the same API as atexit, only shares a name for the sake of communicating intention.
Important Disclaimer:
It should be noted that this package is really intended for cases where you're porting or otherwise migrating an existing asyncio library / application to AnyIO. The idea of unmanaged and unaccountable "global" tasks is antithetical to structured concurrency principles, so should be considered carefully. In most cases, if you have the choice, refactoring using asynchronous context managers and task groups is likely a better long-term solution.
Installation
$ pdm add anyio-atexit
Optional extras:
trioto use in a trio runtime.asyncioto use in an asyncio runtime. This installsasyncio-atexit.
Usage
A common use case is to ensure some free-floating asynchronous resource (like an IO client) gets gracefully closed before the loop exits in order to release a connection.
import anyio
from anyio_atexit import run_finally
class Foo:
def __init__(self):
# protect against users forgetting to close the client
run_finally(self.disconnect)
async def disconnect(self) -> None: ...
async def main():
some_client = Foo()
anyio.run(main)
For anyio AsyncResources, you can also use the ensure_resource_closure function to automatically register the .aclose method, as well as get resource warnings and instantiation stack traces for unclosed resources.
You must implement a sync is_closed(self) -> bool method for the resource to be used with ensure_resource_closure.
Registering a finalizer for a resource does not make the async runtime aware of explicit closures. If you register a finalizer for a resource, you are also responsible for ensuring that the resource can be closed repeatedly without error for when the finalizer runs.
import anyio
from anyio_atexit import ensure_resource_closure
class Foo(AsyncResource):
def __init__(self):
self._closed = False
ensure_resource_closure(self)
def is_closed(self) -> bool:
return self._closed
async def aclose(self) -> None:
if not self.is_closed():
self._closed = True
...
async def main():
some_client = Foo()
anyio.run(main)
License
This software is licensed under the Clear BSD License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file anyio_atexit-1.0.0.tar.gz.
File metadata
- Download URL: anyio_atexit-1.0.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.22.4 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6b7f46c0382dbdb386a792b9f5d6718331298d8fba40f2122a19b2a9522ab42
|
|
| MD5 |
0a13dfb552c851e85579a1052fa3d252
|
|
| BLAKE2b-256 |
39794b7f10647d03b40b45740fa2869b3ddabe1c720538fbdea5ac0a5b691678
|
File details
Details for the file anyio_atexit-1.0.0-py3-none-any.whl.
File metadata
- Download URL: anyio_atexit-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.22.4 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96f32238da79e5c15e5ddd23c125647490dc2c1c6ec713bdbfb3e551521f5214
|
|
| MD5 |
b7a2ea6d8dcb235328fc6eb4e5d3d031
|
|
| BLAKE2b-256 |
166d633b97ef824dc2793e1c54d5588cebfc01f1dbfce6918b135a807d72a39d
|