``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
ref
Create a reference to the current thread, with a callback that will fire when the thread exits:
reference = threadref.ref(lambda reference: ...)
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 just like weakref.ref, threadref.ref instances must still be alive when their referent thread stops running, or their callback will not be called.
finalize
Create a finalizer for the current thread, which will be called when the thread exits:
finalizer = threadref.finalize(function, *args, **kwargs)
threadref.finalize mirrors weakref.finalize, except that:
It references the thread that constructed it instead of taking a referent argument.
In all cases where weakref.finalize returns the tuple (object, function, args, kwargs), it returns the tuple (thread, function, args, kwargs) instead.
It starts returning None once the thread stops running.
The finalizer remains alive on its own as long as it needs to, so this is a simpler and nicer interface in the typical case of registering cleanup functions.
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 Distributions
Hashes for threadref-1.1.1-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be6556474013dfa766f311238129018e395e78fc9b5c3a6b5610c343bebfbcc2 |
|
MD5 | b0eea6a1ab2708a09cd1d86e5c4af3f3 |
|
BLAKE2b-256 | f715f909b34add1cba7c19ce8ba687e259131d94b13efb4fa11d23d8fe6c8285 |
Hashes for threadref-1.1.1-py34-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53f8ce51460b0517ecf81a43c591440121fc49998b31334159968a097f16fb84 |
|
MD5 | e962f3b86e0c7bc80becd1303c8a34cf |
|
BLAKE2b-256 | e49f76e9c6ee150a72b2dec17bbeaf00a67b6e9b25344ea902f39c3c8371902e |
Hashes for threadref-1.1.1-py2.py30-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aeadadbaa4aabd691de23bfa12515e67bd2f0b9cdd851ced6e2e3b3520290571 |
|
MD5 | 16a7c820230a22d52c01f58d81d7eefc |
|
BLAKE2b-256 | 77650cec0c91396e0f0b2b5c919d7efc19477dde953f374bc6b181a1a56e8c87 |