Skip to main content

Pure python callback/event system modeled after Qt Signals

Project description

psygnal

License PyPI Conda Python Version CI codecov

Psygnal (pronounced "signal") is a pure python implementation of Qt-style Signals with (optional) signature and type checking, and support for threading.

Note: this library does not require Qt. It just implements a similar pattern of inter-object communication with loose coupling.

Documentation

https://psygnal.readthedocs.io/

Install

pip install psygnal
conda install -c conda-forge psygnal

Usage

A very simple example:

from psygnal import Signal

class MyObject:
    value_changed = Signal(str)
    shutting_down = Signal()

my_obj = MyObject()

@my_obj.value_changed.connect
def on_change(new_value: str):
    print(f"The value changed to {new_value}!")

my_obj.value_changed.emit('hi')

Much more detail available in the documentation!

Alternatives

There are other libraries that implement similar event-based signals, they may server your purposes better depending on what you are doing.

PySignal (deprecated)

This package borrows inspiration from – and is most similar to – the now deprecated PySignal project, with a few notable new features in psygnal regarding signature and type checking, sender querying, and threading.

similarities with PySignal

  • still a "Qt-style" signal implementation that doesn't depend on Qt
  • supports class methods, functions, lambdas and partials

differences with PySignal

  • the class attribute pysignal.ClassSignal is called simply Signal in psygnal (to more closely match the PyQt/Pyside syntax). Correspondingly pysignal.Signal is similar to psygnal.SignalInstance.
  • Whereas PySignal refrained from doing any signature and/or type checking either at slot-connection time, or at signal emission time, psygnal offers signature declaration similar to Qt with , for example, Signal(int, int). along with opt-in signature compatibility (with check_nargs=True) and type checking (with check_types=True). .connect(..., check_nargs=True) in particular ensures that any slot to connected to a signal will at least be compatible with the emitted arguments.
  • You can query the sender in psygnal by using the Signal.sender() or Signal.current_emitter() class methods. (The former returns the instance emitting the signal, similar to Qt's QObject.sender() method, whereas the latter returns the currently emitting SignalInstance.)
  • There is basic threading support (calling all slots in another thread), using emit(..., asynchronous=True). This is experimental, and while thread-safety is the goal, it is not guaranteed.
  • There are no SignalFactory classes here.

The following two libraries implement django-inspired signals, they do not attempt to mimic the Qt API.

Blinker

Blinker provides a fast dispatching system that allows any number of interested parties to subscribe to events, or "signals".

SmokeSignal

(This appears to be unmaintained)

Benchmark history

https://www.talleylambert.com/psygnal/

Developers

Debugging

While psygnal is a pure python module, it is compiled with Cython to increase performance. To import psygnal in uncompiled mode, without deleting the shared library files from the psyngal module, set the environment variable PSYGNAL_UNCOMPILED before importing psygnal. The psygnal._compiled variable will tell you if you're running the compiled library or not.

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

psygnal-0.4.1.tar.gz (1.1 MB view hashes)

Uploaded source

Built Distributions

psygnal-0.4.1-cp310-cp310-win_amd64.whl (656.5 kB view hashes)

Uploaded cp310

psygnal-0.4.1-cp310-cp310-macosx_11_0_arm64.whl (726.0 kB view hashes)

Uploaded cp310

psygnal-0.4.1-cp39-cp39-win_amd64.whl (657.5 kB view hashes)

Uploaded cp39

psygnal-0.4.1-cp39-cp39-macosx_11_0_arm64.whl (728.7 kB view hashes)

Uploaded cp39

psygnal-0.4.1-cp38-cp38-win_amd64.whl (661.6 kB view hashes)

Uploaded cp38

psygnal-0.4.1-cp38-cp38-macosx_11_0_arm64.whl (735.6 kB view hashes)

Uploaded cp38

psygnal-0.4.1-cp37-cp37m-win_amd64.whl (649.1 kB view hashes)

Uploaded cp37

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page