skillfed

asyncio-atexit

Like atexit, but for asyncio

asyncio-atexit v1.0.1 671.9K downloads/30d#5,403 on PyPI22
Permissive license MIT License Active released

What it is and what it does

asyncio-atexit fills a gap in asyncio's lifecycle management by providing a way to register coroutines and synchronous functions that run when an event loop closes. The standard library's atexit module registers functions to run at interpreter shutdown, but asyncio applications often need cleanup tied to the event loop's lifecycle instead—for example, closing database connections or flushing buffers when a specific loop terminates. This package lets you register those handlers and have them called automatically.

The package accepts both async and sync functions, and they are invoked when loop.close() is called. It's a thin, focused tool: no dependencies, works with Python 3.6 and later, and integrates directly into asyncio's shutdown sequence. The main constraint is that cleanup only runs if your application explicitly closes the loop—it won't trigger on implicit cleanup or if the loop is abandoned.

Use it for:

  • Close database or cache connections when an asyncio application's event loop shuts down
  • Flush pending writes or cleanup temporary resources in async server applications
  • Register teardown logic in async test fixtures that need to run after each event loop
  • Clean up external resources (files, network sockets) tied to a specific event loop instance

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Registers cleanup functions to run when an asyncio event loop closes, similar to the standard library's atexit but tied to event loop lifecycle rather than interpreter exit.

Yes, if you need event-loop-scoped cleanup in asyncio applications. Install friction is minimal and the license is permissive. The main caveat: the package has not been updated since 2022-04-26, so verify compatibility with your Python version and test that cleanup runs as expected in your specific event loop setup. No known vulnerabilities.

Install

asyncio-atexit on PyPI

pip

pip install asyncio-atexit

uv

uv add asyncio-atexit

poetry

poetry add asyncio-atexit

Installing asyncio-atexit

Before you install

Low friction: pure Python wheel with no runtime dependencies. Maintenance is active with recent commits, though the package itself has not been updated since 2022-04-26.

License in practice

MIT License (permissive) — you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.

Quickstart

pip install asyncio-atexit

import asyncio_atexit

async def cleanup():
    print('Cleaning up')

asyncio_atexit.register(cleanup)
# cleanup() runs when the event loop closes

Must be called from within a running event loop, and cleanup only runs if the application explicitly calls loop.close()

Verify before relying

  • Whether the package works correctly with modern Python versions beyond 3.6, given no release since 2022
  • Behavior when multiple event loops are created in the same process
  • Performance impact of the registration mechanism under high-volume cleanup scenarios

Package facts

License MIT License (permissive)
Python support supports the current Python release (>=3.6)
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 1,571 days since the last release
Last repo commit
First released
Downloads 671,892/month — #5,403 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: asyncio_atexit-1.0.1-py3-none-any.whl

Keywords: asyncio

Framework :: AsyncIOLicense :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3.6

Tags

asyncio cleanup on loop closeevent loop atexit equivalentasyncio shutdown handlersregister async cleanup functionsasyncio event loop exit callback
asynciolifecycle-management

More Application Frameworks packages