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 SignalObjects. SignalObjects 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.7.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.7-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

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

Hashes for threadtools-0.0.7.tar.gz
Algorithm Hash digest
SHA256 3c4b6a9483b102d73e58668131964fea66a9a25630c5ef861043886c8c1898b6
MD5 a97644338a401b3e7f3d3d2939d847dd
BLAKE2b-256 4f9e012a9b00de4eec42cd9ad174f1bc700f0065627e7a35c5168f2616354e1e

See more details on using hashes here.

Provenance

The following attestation bundles were made for threadtools-0.0.7.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.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

Hashes for threadtools-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 46cd90f1a3d66b9666cf3dddef6872dd3abee3210f1c59a712946e3e657b86bc
MD5 439af06e825c69160e3f5466e24c53cb
BLAKE2b-256 70e6d2dd6a21224fa0762c47651e61f7f78c5ebdf62a6da84f16e30707f707ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for threadtools-0.0.7-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