Pure python implementation of Qt Signals
Project description
psygnal
Pure python implementation of Qt-style Signals, with (optional) signature and type checking, and support for threading.
Usage
Install
pip install psygnal
Basic usage
If you are familiar with the Qt Signals &
Slots API as implemented in
PySide and
PyQt5,
then you should be good to go! psygnal
aims to be a superset of those APIs
(some functions do accept additional arguments, like
check_nargs
and
check_types
).
Note: the name "Signal
" is used here instead of pyqtSignal
, following the
qtpy
and PySide
convention.
from psygnal import Signal
# create an object with class attribute Signals
class MyObj:
# this signal will emit a single string
value_changed = Signal(str)
def __init__(self, value=0):
self._value = value
def set_value(self, value):
if value != self._value:
self._value = str(value)
# emit the signal
self.value_changed.emit(self._value)
def on_value_changed(new_value):
print(f"The new value is {new_value!r}")
# instantiate the object with Signals
obj = MyObj()
# connect one or more callbacks with `connect`
obj.value_changed.connect(on_value_changed)
# callbacks are called when value changes
obj.set_value('hello!') # prints: 'The new value is 'hello!'
# disconnect callbacks with `disconnect`
obj.value_changed.disconnect(on_value_changed)
connect
as a decorator
.connect()
returns the object that it is passed, and so
can be used as a decorator.
@obj.value_changed.connect
def some_other_callback(value):
print(f"I also received: {value!r}")
obj.set_value('world!')
# prints:
# I also received: 'world!'
Connection safety (number of arguments)
psygnal
prevents you from connecting a callback function that is guaranteed
to fail due to an incompatible number of positional arguments. For example,
the following callback has too many arguments for our Signal (which we declared
above as emitting a single argument: Signal(str)
)
def i_require_two_arguments(first, second):
print(first, second)
obj.value_changed.connect(i_require_two_arguments)
raises:
ValueError: Cannot connect slot 'i_require_two_arguments' with signature: (first, second):
- Slot requires at least 2 positional arguments, but spec only provides 1
Accepted signature: (p0: str, /)
Note: Positional argument checking can be disabled with connect(..., check_nargs=False)
Extra positional arguments ignored
While a callback may not require more positional arguments than the signature
of the Signal
to which it is connecting, it may accept less. Extra
arguments will be discarded when emitting the signal (so it
isn't necessary to create a lambda
to swallow unnecessary arguments):
obj = MyObj()
def no_args_please():
print(locals())
obj.value_changed.connect(no_args_please)
# otherwise one might need
# obj.value_changed.connect(lambda a: no_args_please())
obj.value_changed.emit('hi') # prints: "{}"
Connection safety (types)
For type safety when connecting slots, use check_types=True
when connecting a
callback. Recall that our signal was declared as accepting a string
Signal(str)
. The following function has an incompatible type annotation: x: int
.
# this would fail because you cannot concatenate a string and int
def i_expect_an_integer(x: int):
print(f'{x} + 4 = {x + 4}')
# psygnal won't let you connect it
obj.value_changed.connect(i_expect_an_integer, check_types=True)
raises:
ValueError: Cannot connect slot 'i_expect_an_integer' with signature: (x: int):
- Slot types (x: int) do not match types in signal.
Accepted signature: (p0: str, /)
Note: unlike Qt, psygnal
does not perform any type coercion when emitting
a value.
Connection safety (object references)
psygnal tries very hard not to hold strong references to connected objects. In the simplest case, if you connect a bound method as a callback to a signal instance:
class T:
def my_method(self):
...
obj = T()
signal.connect(t.my_method)
Then there is a risk of signal
holding a reference to obj
even after obj
has been deleted, preventing garbage collection (and possibly causing errors
when the signal is emitted next). Psygnal avoids this with weak references. It
goes a bit farther, trying to prevent strong references in these cases as well:
- class methods used as the callable in
functools.partial
- decorated class methods that mangle the name of the callback.
Another common case for leaking strong references is a partial
closing on an
object, in order to set an attribute:
class T:
x = 1
obj = T()
signal.connect(partial(setattr, obj, 'x')) # ref to obj stuck in the connection
Here, psygnal offers the connect_settatr
convenience method, which reduces code
and helps you avoid leaking strong references to obj
:
signal.connect_setatttr(obj, 'x')
Query the sender
Similar to Qt's QObject.sender()
method, a callback can query the sender using the Signal.sender()
class
method. (The implementation is of course different than Qt, since the receiver
is not a QObject
.)
obj = MyObj()
def curious():
print("Sent by", Signal.sender())
assert Signal.sender() == obj
obj.value_changed.connect(curious)
obj.value_changed.emit(10)
# prints (and does not raise):
# Sent by <__main__.MyObj object at 0x1046a30d0>
If you want the actual signal instance that is emitting the signal
(obj.value_changed
in the above example), use Signal.current_emitter()
.
Emitting signals asynchronously (threading)
There is experimental support for calling all connected slots in another thread,
using emit(..., asynchronous=True)
obj = MyObj()
def slow_callback(arg):
import time
time.sleep(0.5)
print(f"Hi {arg!r}, from another thread")
obj.value_changed.connect(slow_callback)
This one is called synchronously (note the order of print statements):
obj.value_changed.emit('friend')
print("Hi, from main thread.")
# after 0.5 seconds, prints:
# Hi 'friend', from another thread
# Hi, from main thread.
This one is called asynchronously, and immediately returns to the caller.
A threading.Thread
object is returned.
thread = obj.value_changed.emit('friend', asynchronous=True)
print("Hi, from main thread.")
# immediately prints
# Hi, from main thread.
# then after 0.5 seconds this will print:
# Hi 'friend', from another thread
Note: The user is responsible for joining
and managing the
threading.Thread
instance returned when calling .emit(..., asynchronous=True)
.
Experimental! While thread-safety is the goal,
(RLocks
are
used during important state mutations) it is not guaranteed. Please use at your
own risk. Issues/PRs welcome.
Blocking a signal
To temporarily block a signal, use the signal.blocked()
context context manager:
obj = MyObj()
with obj.value_changed.blocked():
# do stuff without obj.value_changed getting emitted
...
To block/unblock permanently (outside of a context manager), use signal.block()
and signal.unblock()
.
Pausing a signal
Sometimes it is useful to temporarily collect/buffer emission events, and then emit
them together as a single event. This can be accomplished using the
signal.pause()
/signal.resume()
methods, or the signal.paused()
context manager.
If a function is passed to signal.paused(func)
(or signal.resume(func)
) it will
be passed to functools.reduce
to combine all of the emitted values collected during
the paused period, and a single combined value will be emitted.
obj = MyObj()
obj.value_changed.connect(print)
# note that signal.paused() and signal.resume() accept a reducer function
with obj.value_changed.paused(lambda a,b: (f'{a[0]}_{b[0]}',), ('',)):
obj.value_changed('a')
obj.value_changed('b')
obj.value_changed('c')
# prints '_a_b_c'
NOTE: args passed to emit
are collected as tuples, so the two arguments
passed to reducer
will always be tuples. reducer
must handle that and
return an args tuple.
For example, the three emit()
events above would be collected as
[('a',), ('b',), ('c',)]
and would be reduced and re-emitted as follows:
obj.emit(*functools.reduce(reducer, [('a',), ('b',), ('c',)]))
Other similar libraries
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 simplySignal
inpsygnal
(to more closely match the PyQt/Pyside syntax). Correspondinglypysignal.Signal
is similar topsygnal.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 (withcheck_nargs=True
) and type checking (withcheck_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 theSignal.sender()
orSignal.current_emitter()
class methods. (The former returns the instance emitting the signal, similar to Qt'sQObject.sender()
method, whereas the latter returns the currently emittingSignalInstance
.) - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for psygnal-0.3.3-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15f93302512e110217ea64fef356134c73d6b568c629bef6b7e9501119e415b3 |
|
MD5 | e6368c761a7a8b19d7f8d7eb602011e6 |
|
BLAKE2b-256 | 1232aeced00b2f680a850fa09ef199f74dd6f9687afe9b9954c225e1f7c2fd2b |
Hashes for psygnal-0.3.3-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 062b96fc98c45daf756834e985b393419f710b9d063af9d678691e946b8b34da |
|
MD5 | 27ace6c9ee43ca549a35181529533b60 |
|
BLAKE2b-256 | c33be5f3c5b526ecd7abc18d81f26ddf3039ba8c4bbbfeeedede5744602d090d |
Hashes for psygnal-0.3.3-cp310-cp310-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6adf531d7004aa7e3c3723ee81de4d493d67475924af3d40d692a4d6bacf26f0 |
|
MD5 | 70a8310df08280ae1c4bee739456331a |
|
BLAKE2b-256 | ac185a4b53e2a75bfe3b2422410dc75920c52612590804c0445f86321d7ff2a0 |
Hashes for psygnal-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee1a9f80691dfbd792ceee4a2ec5b6a533b2b44bc292c6c54334647a8423bbc0 |
|
MD5 | 34348d906fec850366723e9eab6e08a5 |
|
BLAKE2b-256 | 94a37015bb0807a655c848f44ac00d8b5f7033193cff5fd921e963b513975588 |
Hashes for psygnal-0.3.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34ba8a64cc6f6f93812ffc25ff6b6c8b983b9b9ea26c6c09f2d8766feefff732 |
|
MD5 | c4429b37e3c70a48595290b8c22ef56f |
|
BLAKE2b-256 | d3ddb7a3e548d3423ad305d6ccdd17aa0b57621598fc46612c35bebe0a06503f |
Hashes for psygnal-0.3.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 145555a3b0cc2911e21b1edc0a5b1c89fa224ab70b805e47921c19589ca355e1 |
|
MD5 | a3c71bc7cb35d55ed607d2581740a669 |
|
BLAKE2b-256 | 5095269e3f1ede046f881a6e34506564659ab85fc0f1658c5622a20c149a719f |
Hashes for psygnal-0.3.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3dbb038490f31ab86e85ab29a2af7341f4b0d85cf99463c797d3c5de6786149f |
|
MD5 | 3f4c6ef69b81bee7ad15109150f29a12 |
|
BLAKE2b-256 | 947bd5dcecf051122dbf04ba0e8da9ba11cf20619f86a8069c0e2197748d743c |
Hashes for psygnal-0.3.3-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac4fbb1bb54ea775070cf906d5ca8009b80e67c9feedb9df663e4dd3d408e806 |
|
MD5 | 48a0cb546d5d2d932188a5daf811d11e |
|
BLAKE2b-256 | ee88a69921ead0613f680ec736673cff0d0669531dcd4d1243f56eac032e383c |
Hashes for psygnal-0.3.3-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 848ddb7fe3aaa553cac14f23562224728539d51b107c2803d657ffebccc17124 |
|
MD5 | 2773dec1aa5c59c032cc6d232de366c4 |
|
BLAKE2b-256 | 54035786ac242d04884dbeb9e07dfdfa01c7b7a100d6e4add54da91f4c5b3745 |
Hashes for psygnal-0.3.3-cp39-cp39-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6bb551ba483defa806fe8b3db41d36081bd9373c523b88392ddd7553a570091b |
|
MD5 | 82822fc4eccfc144ddc20f8caa0a9bfd |
|
BLAKE2b-256 | 0b50b8f04fb0ee787f1621761d6cb72db36af32310be5e16e3f49f27251202f9 |
Hashes for psygnal-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21869d4fb7f31c21e31c430a31b8455342b0e0f990fecca9242aaa67d6a545db |
|
MD5 | 632801fbffc5d35fc632d20063c18ae8 |
|
BLAKE2b-256 | e21a95d7e0f55c8cf912e7f9101475c73b7728866c014039aa969843f60ee3b0 |
Hashes for psygnal-0.3.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d486fc1ca7dbd590822119557b5f5ff4268bb04c76ae9964d3233c831308f55 |
|
MD5 | 1046293f71434292964045f6caeb6e6a |
|
BLAKE2b-256 | 520f35bd7658e48c5610114707ffd05e2d1fa2fda06f1c977420ece5b17d4ae1 |
Hashes for psygnal-0.3.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee6bc894f32bcd8933f6813e5d25788ea677ed2923bca3ab6f23cbf2ec5ce110 |
|
MD5 | 6e3f6c8f80afd3384aa96dbaa5727b0f |
|
BLAKE2b-256 | e0334ad9d12bf9d5a572156a928f9c4eb18fe98000e6d54e20c4c1e9fd4c473b |
Hashes for psygnal-0.3.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ad353ccae8f31dbc0dbac3516029e2bea20ff16b9bf754943c9740f03653cbc |
|
MD5 | 8d7cd34b4b8f689c45beb20d3f95c346 |
|
BLAKE2b-256 | 610cc3660ec3c7c54727b72b832bf487cbf76fee405d4b866991c1121acd73e5 |
Hashes for psygnal-0.3.3-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b8912a669460dcf0b0db9f27e13984b8cac210cf0ace9bcd33198ae3627f81d |
|
MD5 | 7462eb48223b66b8ca44a07794f2563c |
|
BLAKE2b-256 | 22128ed114624d1cba313ea930932310b97b20e66a6ee696c2d18c6fb3207563 |
Hashes for psygnal-0.3.3-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c87ebe5833956431ccfc14de831e03b31ea830e5a0332c377ca744a4d7eedd1 |
|
MD5 | b5e5b4a1a02f4839496e2af6732f3bc4 |
|
BLAKE2b-256 | efb19f3b7bfab6832382989fdbb3f34f13b0a72fedf614631c8743989c25e56a |
Hashes for psygnal-0.3.3-cp38-cp38-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e71a9a352fe10e051f9a00f0dfcda08262dd71096801bd24f79e6a12222ffecb |
|
MD5 | d55c71ad39e91ee157d5677986d3a424 |
|
BLAKE2b-256 | 1ff9589097f2115188cea3504278db3240dac25d62f3aadbd85dc07a3d9726db |
Hashes for psygnal-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca040388153cfa4479e703e2404736f57e0a1c237337cf216a54cb729f94e539 |
|
MD5 | 9eb9ef8c3fbf330f75aec8d6fd2f689b |
|
BLAKE2b-256 | 672c0cc2a24d774ee3aa08dfbcd606e0f9b59afa77a7db874770b6eb45e62369 |
Hashes for psygnal-0.3.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 006aa6ec8d6f42a013c68c9d64c3623053c2b671cf9a8ddccc40abc9ff3dece0 |
|
MD5 | 53f455ce0e4272318d1be7a3f5d89f39 |
|
BLAKE2b-256 | 5cfb03bf9953f228d6c5c7264376038ff6a52ef91f4ae83ec0ee190fa9635bdf |
Hashes for psygnal-0.3.3-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eeea1063a3fa268f12e8787711c0bff44cba90183a9f43db70c11365e24d5e15 |
|
MD5 | 0ef90eee0084600e22d0dd9adc04617e |
|
BLAKE2b-256 | c19609edf4eae99f1f3d6d4d2804ddd1bbec8897c73862f9db00dbae9eb58c9c |
Hashes for psygnal-0.3.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc60641edcd45625be83f9f45ff57501a7338ba9001d5dd717db0843ee178c66 |
|
MD5 | 7d2cde148043b1ada78d3f8ab252eff8 |
|
BLAKE2b-256 | 6c68cda28b710ea12a9f50a850004f9af06508dac0ce050bc04ae291ef45e847 |
Hashes for psygnal-0.3.3-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46a16423086d79ac1836c4a64d59c5fb7bd534e6af3418cabf5d7c37d50d0399 |
|
MD5 | c104c78b54cd029fb7dca1da65b7af94 |
|
BLAKE2b-256 | a80319ce67de10cdeb8c279496234218b32e47df7319247ec586bed9bea9ac7a |
Hashes for psygnal-0.3.3-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5fd8f916cbba09bca5669f783e8fc1972f3997399e40ade2d06791256a64260 |
|
MD5 | e5831bb25f0b04cc86363d305ef23087 |
|
BLAKE2b-256 | 918c128699b6de6da9c29cec85bda1f5c5d69344c3a08a7df465d16324f0df22 |
Hashes for psygnal-0.3.3-cp37-cp37m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 140aaf7827afbef6001a6dc93e414e7ae759bb4721342f540d1227fb70b2531f |
|
MD5 | e684fc7e8c3d021e1606711ed7618672 |
|
BLAKE2b-256 | 73d236370c83309fac9819a5ddd0712cfe9494f888e648d2c4d9498b2c415bc9 |
Hashes for psygnal-0.3.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41ce602ddcb7d73166269cbe75a12a3e5874f0f8c332e28ed8c33b171ada8939 |
|
MD5 | 8306e1c0fff3691eef4e0b39e426d577 |
|
BLAKE2b-256 | cb6e3a0cbf6719e84a15ceb077d9df5482f69eb99140141a6205fe8198781b73 |
Hashes for psygnal-0.3.3-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b1230ae5ab3140089aeb0f3afe24e02490b824e2d5d36442b21df62aae3a224 |
|
MD5 | 0e53d22f76e78a0c52defcceaf1edc06 |
|
BLAKE2b-256 | 13bfd9460669d3e6323c8c7126fc91c6907ad1c109347e4cc474f5306c4a03bb |
Hashes for psygnal-0.3.3-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2dd7622d9ec571acd66240686b1ef5323b3934e53d4aed6c83c9b86ab31b9053 |
|
MD5 | b500eb17eec097f7674ab9d7298bd604 |
|
BLAKE2b-256 | a791603261e01337ea89011b879684517fb81a0becad01dcb73b8c1bbdcf0d75 |