Tools for synchronizing threads using better locks and signals.
Project description
threadtools
Support for signals and better locks in native Python.
Inspiration
PyQt lets you "emit" signals that have function callbacks tied to them. Why shouldn't we have that in native Python?
Basic Usage
import time
from threading import Thread
from threadtools import Signal, SignalReceiver, process_events
class ThreadedProcess:
def __init__(self):
self.somethingHappened = Signal[str]()
self.countChanged = Signal[int]()
self.finished = Signal() # no typing implies no arguments to `emit()`
def run(self):
"""Mimics a long-running process that updates its progress."""
for i in range(1, 6):
time.sleep(1)
self.countChanged.emit(i)
if i == 3:
self.somethingHappened.emit("something happened!")
self.finished.emit()
receiver = SignalReceiver()
threaded_process = ThreadedProcess()
thread = Thread(target=threaded_process.run)
# connect signals
threaded_process.countChanged.connect(receiver, print)
threaded_process.somethingHappened.connect(receiver, print)
threaded_process.finished.connect(receiver, lambda: print("done!"))
# run the thread
thread.start()
# you must call `process_events()` to receive signals from other threads
# `receiver` was created in a different thread than `thread`, so signal
# callbacks are queued rather than being run immediately
while thread.is_alive():
process_events()
# prints:
# 1
# 2
# 3
# something happened!
# 4
# 5
# done!
Thread Safety
threadtools is designed to be thread-safe as long as signals are connected to the correct recipient. Recipients must be SignalObjects. SignalObjects can be associated with a different thread using their move_to_thread() method.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file threadtools-0.0.7.tar.gz.
File metadata
- Download URL: threadtools-0.0.7.tar.gz
- Upload date:
- Size: 17.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c4b6a9483b102d73e58668131964fea66a9a25630c5ef861043886c8c1898b6
|
|
| MD5 |
a97644338a401b3e7f3d3d2939d847dd
|
|
| BLAKE2b-256 |
4f9e012a9b00de4eec42cd9ad174f1bc700f0065627e7a35c5168f2616354e1e
|
Provenance
The following attestation bundles were made for threadtools-0.0.7.tar.gz:
Publisher:
pypi.yml on drewlwhitney/threadtools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
threadtools-0.0.7.tar.gz -
Subject digest:
3c4b6a9483b102d73e58668131964fea66a9a25630c5ef861043886c8c1898b6 - Sigstore transparency entry: 230484645
- Sigstore integration time:
-
Permalink:
drewlwhitney/threadtools@d61b3eeb2db33bf75a93f097fc391c5774b232ce -
Branch / Tag:
refs/tags/v0.0.7 - Owner: https://github.com/drewlwhitney
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@d61b3eeb2db33bf75a93f097fc391c5774b232ce -
Trigger Event:
release
-
Statement type:
File details
Details for the file threadtools-0.0.7-py3-none-any.whl.
File metadata
- Download URL: threadtools-0.0.7-py3-none-any.whl
- Upload date:
- Size: 18.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46cd90f1a3d66b9666cf3dddef6872dd3abee3210f1c59a712946e3e657b86bc
|
|
| MD5 |
439af06e825c69160e3f5466e24c53cb
|
|
| BLAKE2b-256 |
70e6d2dd6a21224fa0762c47651e61f7f78c5ebdf62a6da84f16e30707f707ab
|
Provenance
The following attestation bundles were made for threadtools-0.0.7-py3-none-any.whl:
Publisher:
pypi.yml on drewlwhitney/threadtools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
threadtools-0.0.7-py3-none-any.whl -
Subject digest:
46cd90f1a3d66b9666cf3dddef6872dd3abee3210f1c59a712946e3e657b86bc - Sigstore transparency entry: 230484646
- Sigstore integration time:
-
Permalink:
drewlwhitney/threadtools@d61b3eeb2db33bf75a93f097fc391c5774b232ce -
Branch / Tag:
refs/tags/v0.0.7 - Owner: https://github.com/drewlwhitney
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@d61b3eeb2db33bf75a93f097fc391c5774b232ce -
Trigger Event:
release
-
Statement type: