Skip to content

Support closing Kv objects manually and automatically#20

Merged
h4l merged 5 commits intomainfrom
kv-auto-close
Feb 1, 2025
Merged

Support closing Kv objects manually and automatically#20
h4l merged 5 commits intomainfrom
kv-auto-close

Conversation

@h4l
Copy link
Owner

@h4l h4l commented Feb 1, 2025

This implements support for closing Kv client via:

  • Kv.aclose()
  • async context manager
  • At interpreter exit / garbage collection via Kv.create_finalizer()
  • Automatically when an interactive console exists:
    • Kv objects created by open_kv() from an interactive console/REPL automatically close at exit.
    • The open_kv() function has a finalize option that controls this.

h4l added 5 commits February 1, 2025 15:14
Kv now has a closed property, aclose() method and is an async context
manager, to support auto-closing. (Previously the aiohttp session would
need to be closed.)
The Kv.create_finalizer() method returns a finalizer that automatically
closes the instance when it goes out of scope, or at exit. This allows
for auto-cleanup of Kv instances in environments where it's awkward to
use a context block, like an interactive REPL.
When running in Python REPLs, Kv instances now automatically close at
exit. This avoids the aiohttp.ClientSession printing warnings about
un-closed connections when exiting a REPL.
There are never going to be many instances at once, no point in using
slots. (We need to support weakrefs of Kv, and using slots complicates
this.)
@h4l h4l merged commit 33fa684 into main Feb 1, 2025
23 checks passed
@h4l h4l deleted the kv-auto-close branch February 2, 2025 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant