Signals and slots implementation with asyncio support
Project description
Signals and slots implementation with asyncio support
Slots can be functions, methods or coroutines. Weak references are used by default. If slot is coroutine, it will be scheduled to run asynchronously with asyncio.async() (but you must run event loop by yourself).
You can also run blocking functions asynchronously by specifying force_async=True when connecting signal to slot (it will only apply to that slot) or when creating signal (it will apply to all connected slots). ThreadPoolExecutor with 5 worker threads is used by default, but it can be changed when creating signal with executor argument.
Requirements
Python >= 3.4
Usage
Example:
from signaller import Signal
logging.basicConfig(level=logging.DEBUG)
def slot(arg):
print('slot:', arg)
# Creating signals (you can set signal name, but it is not required,
# signals can be anonymous):
sig_test = Signal('sig_test')
# Connecting signals to slots (uses weak references by default,
# but you can force strong references by specifying weak=False):
sig_test.connect(slot)
sig_test.connect(lambda arg: print('slot_lambda:', arg), weak=False)
# You can also use decorators for connecting signals to slots:
@sig_test.connect
def slot2(arg):
print('slot2:', arg)
# And weak=False can be specified when using decorators too:
@sig_test.connect(weak=False)
def slot3(arg):
print('slot3:', arg)
# Slots are automatically disconnected from signals
# when using weak references:
del slot2
# Or you can disconnect slots manually:
sig_test.disconnect(slot3)
# Emitting signals (you can send positional and keyword
# arguments to connected slots):
sig_test.emit('Hello world!')
Output:
INFO:__main__:Connecting signal <Signal 'sig_test' at 0x7fc31dcdef98> to slot <function slot at 0x7fc31dceeae8> INFO:__main__:Connecting signal <Signal 'sig_test' at 0x7fc31dcdef98> to slot <function <lambda> at 0x7fc31b03a1e0> INFO:__main__:Connecting signal <Signal 'sig_test' at 0x7fc31dcdef98> to slot <function slot2 at 0x7fc31b03a2f0> INFO:__main__:Connecting signal <Signal 'sig_test' at 0x7fc31dcdef98> to slot <function slot3 at 0x7fc31b03a488> DEBUG:__main__:Object <function slot2 at 0x7fc31b03a2f0> has been deleted INFO:__main__:Disconnecting slot <Reference (weak) to <function slot2 at 0x7fc31b03a2f0> (dead)> from signal <Signal 'sig_test' at 0x7fc31dcdef98> INFO:__main__:Disconnecting slot <function slot3 at 0x7fc31b03a488> from signal <Signal 'sig_test' at 0x7fc31dcdef98> INFO:__main__:Emitting signal <Signal 'sig_test' at 0x7fc31dcdef98> DEBUG:__main__:Calling slot <Reference (weak) to <function slot at 0x7fc31dceeae8>> slot: Hello world! DEBUG:__main__:Calling slot <Reference (strong) to <function <lambda> at 0x7fc31b03a1e0>> slot_lambda: Hello world!
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
File details
Details for the file Signaller-1.0.0.tar.gz
.
File metadata
- Download URL: Signaller-1.0.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6893ef5073573f448c9444ae810b1e4feb0188293ba5d58bafe4b7722e751217 |
|
MD5 | 373131178d8363432cc052371fe3e208 |
|
BLAKE2b-256 | 87eff933bdedc2d60894ecb96d825d1d70fde290aa996c73f7090ba55870e0e2 |