``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
File details
Details for the file threadref-1.1.5.tar.gz
.
File metadata
- Download URL: threadref-1.1.5.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58cb4ad64f1fb451a49e7997762d9a6f876c919ebf386f5ed415b7d6176f7e98 |
|
MD5 | 0f8bb4c0994c84cf9c780a87919673c0 |
|
BLAKE2b-256 | e9acd43ba260fccf4d6d1804d350df9ebcfaa923625f82bf4076d3fbceef3637 |
File details
Details for the file threadref-1.1.5-py38-none-any.whl
.
File metadata
- Download URL: threadref-1.1.5-py38-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4035ff2e238caf7f26afb3aa21062a188601e2f4b3fd8992288a1630e723f398 |
|
MD5 | d08661ceb562074c50b877bfe85efeb4 |
|
BLAKE2b-256 | e22c87ffd6d94e226fe816908f1782b70fea6d0d808213615772d0604af9ee01 |
File details
Details for the file threadref-1.1.5-py34-none-any.whl
.
File metadata
- Download URL: threadref-1.1.5-py34-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3.4
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4be171125669908aea019fb3319cf16e55a136f700af8a231f81657b5da52745 |
|
MD5 | edb45e6973fcc1094a8fa3c2409db32b |
|
BLAKE2b-256 | 84fce19b5550c9c9464d142b8e94a1ed08298a0431b2f007347526f5ca30e6d7 |
File details
Details for the file threadref-1.1.5-py2.py30-none-any.whl
.
File metadata
- Download URL: threadref-1.1.5-py2.py30-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 2, Python 3.0
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aae1865d189d7d3cc239ddac12648f17846654f958286b6f75c226b8f0d7903f |
|
MD5 | 8266ca44e7575a0806e8f0ce69955852 |
|
BLAKE2b-256 | 261213a29911bcababcbf93d16a6e104370c2509a5330ccfbdad9eff85bdc381 |