Skip to main content

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 SignalReceivers. SignalReceivers can be associated with a different thread using their move_to_thread() method.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

threadtools-0.0.8.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

threadtools-0.0.8-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file threadtools-0.0.8.tar.gz.

File metadata

  • Download URL: threadtools-0.0.8.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

Hashes for threadtools-0.0.8.tar.gz
Algorithm Hash digest
SHA256 b8cbef04f3f6e75ef2b6669b3a5aadc3ff78675c9c6b1663248926ff2f124dcd
MD5 87d216d9fa69cf7aa023a422fc1f832a
BLAKE2b-256 ad722802d828c49d5993e4514d594b28980fc33924ef157277a2ccb173c180db

See more details on using hashes here.

Provenance

The following attestation bundles were made for threadtools-0.0.8.tar.gz:

Publisher: pypi.yml on drewlwhitney/threadtools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file threadtools-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: threadtools-0.0.8-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

Hashes for threadtools-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 f48ea1f40b1c7a4bf61fb2c22ac0931d7376ffca7674f7bd817c114c707136e8
MD5 b4eed5e108c2f325f24ebaca94ef9170
BLAKE2b-256 7492ada9dcc0322da83d0ac0df7a7d0ab079a69a5a3960aa3defb88f6ff0baa0

See more details on using hashes here.

Provenance

The following attestation bundles were made for threadtools-0.0.8-py3-none-any.whl:

Publisher: pypi.yml on drewlwhitney/threadtools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page