Skip to main content

anyio-atexit

GitHub Workflow Status PyPI - Downloads GitHub

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:

  • trio to use in a trio runtime.
  • asyncio to use in an asyncio runtime. This installs asyncio-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.

Metadata

Release files for anyio-atexit 1.0.0

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

Source distribution (sdist)

Source distribution for anyio-atexit 1.0.0
File Size Uploaded Provenance Actions
anyio_atexit-1.0.0.tar.gz 5.1 kB Without provenance Details for anyio_atexit-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for anyio-atexit 1.0.0
File Interpreter ABI Platform Actions
anyio_atexit-1.0.0-py3-none-any.whl Python 3 none any Details for anyio_atexit-1.0.0-py3-none-any.whl

Total release size: 10.4 kB

Release files / anyio_atexit-1.0.0.tar.gz

Download URL anyio_atexit-1.0.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a6b7f46c0382dbdb386a792b9f5d6718331298d8fba40f2122a19b2a9522ab42
BLAKE2b-256 checksum
How to use checksums
39794b7f10647d03b40b45740fa2869b3ddabe1c720538fbdea5ac0a5b691678
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.22.4 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2

Release files / anyio_atexit-1.0.0-py3-none-any.whl

Download URL anyio_atexit-1.0.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
96f32238da79e5c15e5ddd23c125647490dc2c1c6ec713bdbfb3e551521f5214
BLAKE2b-256 checksum
How to use checksums
166d633b97ef824dc2793e1c54d5588cebfc01f1dbfce6918b135a807d72a39d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.22.4 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2

Security for anyio-atexit 1.0.0

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.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

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