``weakref`` for threads.
Project description
Allows threads in Python to create “weak references” to themselves that detect when the thread is no longer running, similar to how a weak reference detects when its referent object is no longer alive.
Provides a lightweight way for one or more independent pieces of code to register per-thread cleanup callbacks without coordination.
Versioning
This library’s version numbers follow the SemVer 2.0.0 specification.
Installation
pip install threadref
Usage
Import:
import threadref
Create a reference to the current thread, with a callback that will fire when the thread exits:
ref = threadref.ref(lambda ref: ...)
threadref.ref mirrors weakref.ref, except that:
It references the thread that constructed it instead of taking a referent argument.
It starts returning None instead of the threading.Thread object for its thread once the thread stops running, not once that object stops being alive.
So all weakref.ref caveats apply. In particular, threadref.ref instances must still be alive when their referent thread stops running, or their callback will not be called.
Portability
Internally, threadref is just a weak reference to a thread local variable, and this trick seems to only work on CPython implementations with the C implementation of threading.local.
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
Hashes for threadref-1.0.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ea6fa5bbf127d269e9e74196e50ddaf53effb8929bf0da1e63750cdf6120089 |
|
MD5 | f9dfdad88de2afa73d1e55aea7203771 |
|
BLAKE2b-256 | 9d212657ff053014c0f69bb24dfc613a18f717c75e7ea8a6660a5c6e1002ccd5 |