Skip to main content

Fast python callback/event system modeled after Qt Signals

Project description


License PyPI Conda Python Version CI codecov Documentation Status Benchmarks

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

This library does not require or use Qt in any way, It simply implements a similar observer pattern API.



pip install psygnal
conda install -c conda-forge psygnal


The observer pattern is a software design pattern in which an object maintains a list of its dependents ("observers"), and notifies them of any state changes – usually by calling a callback function provided by the observer.

Here is a simple example of using psygnal:

from psygnal import Signal

class MyObject:
    # define one or more signals as class attributes
    value_changed = Signal(str)

# create an instance
my_obj = MyObject()

# You (or others) can connect callbacks to your signals
def on_change(new_value: str):
    print(f"The value changed to {new_value}!")

# The object may now emit signals when appropriate,
# (for example in a setter method)
my_obj.value_changed.emit('hi')  # prints "The value changed to hi!"

Much more detail available in the documentation!

Evented Dataclasses

A particularly nice usage of the signal pattern is to emit signals whenever a field of a dataclass changes. Psygnal provides an @evented decorator that will emit a signal whenever a field changes. It is compatible with dataclasses from the standard library, as well as attrs, and pydantic:

from psygnal import evented
from dataclasses import dataclass

class Person:
    name: str
    age: int = 0

person = Person('John', age=30)

# connect callbacks
def _on_age_change(new_age: str):
    print(f"Age changed to {new_age}")

person.age = 31  # prints: Age changed to 31

See the dataclass documentation for more details.

Benchmark history




While psygnal is a pure python module, it is compiled with mypyc to increase performance. To disable all compiled files and run the pure python version, you may run:

python -c "import psygnal.utils; psygnal.utils.decompile()"

To return the compiled version, run:

python -c "import psygnal.utils; psygnal.utils.recompile()"

The psygnal._compiled variable will tell you if you're using the compiled version 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.9.5.tar.gz (84.7 kB view hashes)

Uploaded source

Built Distributions

psygnal-0.9.5-py3-none-any.whl (72.0 kB view hashes)

Uploaded py3

psygnal-0.9.5-cp311-cp311-win_amd64.whl (319.5 kB view hashes)

Uploaded cp311

psygnal-0.9.5-cp310-cp310-win_amd64.whl (314.9 kB view hashes)

Uploaded cp310

psygnal-0.9.5-cp39-cp39-win_amd64.whl (314.4 kB view hashes)

Uploaded cp39

psygnal-0.9.5-cp38-cp38-win_amd64.whl (309.7 kB view hashes)

Uploaded cp38

psygnal-0.9.5-cp37-cp37m-win_amd64.whl (302.4 kB view hashes)

Uploaded cp37

Supported by

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