``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 threadref
Create a reference to the current thread, with a callback that will fire when the thread exits:
reference = threadref.ref(lambda reference: ...)
Just like weakref.ref, threadref.ref instances must still be alive when their referent thread stops running, or their callback will not be called.
Create a finalizer for the current thread, which will be called when the thread exits:
finalizer = threadref.finalize(function, *args, **kwargs)
Just like weakref.finalize, threadref.finalize instances remain alive on their own as long as they need to, so this is a simpler and nicer interface in the typical case of registering cleanup functions.
Details
threadref.ref and threadref.finalize wrap weakref.ref and weakref.finalize, and the interface is the same except that they act as if they are referencing the thread itself instead of taking a referent argument, and internally they work by referencing an object saved on a private threading.local instance.
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.5-py38-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4035ff2e238caf7f26afb3aa21062a188601e2f4b3fd8992288a1630e723f398 |
|
MD5 | d08661ceb562074c50b877bfe85efeb4 |
|
BLAKE2b-256 | e22c87ffd6d94e226fe816908f1782b70fea6d0d808213615772d0604af9ee01 |
Hashes for threadref-1.1.5-py34-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4be171125669908aea019fb3319cf16e55a136f700af8a231f81657b5da52745 |
|
MD5 | edb45e6973fcc1094a8fa3c2409db32b |
|
BLAKE2b-256 | 84fce19b5550c9c9464d142b8e94a1ed08298a0431b2f007347526f5ca30e6d7 |
Hashes for threadref-1.1.5-py2.py30-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aae1865d189d7d3cc239ddac12648f17846654f958286b6f75c226b8f0d7903f |
|
MD5 | 8266ca44e7575a0806e8f0ce69955852 |
|
BLAKE2b-256 | 261213a29911bcababcbf93d16a6e104370c2509a5330ccfbdad9eff85bdc381 |