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.9.tar.gz (17.8 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.9-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: threadtools-0.0.9.tar.gz
  • Upload date:
  • Size: 17.8 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.9.tar.gz
Algorithm Hash digest
SHA256 8efaf5889d38e282a7ea799145ea429397d08c450a5e3a90a7853816188c45b3
MD5 6ef653dd04463dc1fca8b55662e13379
BLAKE2b-256 e1404b3561bd7ce958fafef8c0a02177fd3379e0054ded7669024c72bb817ced

See more details on using hashes here.

Provenance

The following attestation bundles were made for threadtools-0.0.9.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.9-py3-none-any.whl.

File metadata

  • Download URL: threadtools-0.0.9-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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 5505f5cbfff71e38fac6cd227283860f465f8c0d1dac1ff9ff16fb0c9b71ca69
MD5 51be90721ad207002f18a3dc9cf07a21
BLAKE2b-256 ad4534bab61a7bde1f43c5285d405f68bd3b252626721b6f2e8088b8c7e38c00

See more details on using hashes here.

Provenance

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