Aioevt
Simplified Asyncio-Friendly Event Management
Problem
Asyncio offers a lot of utilities that provide thread-safe execution of coroutines and synchronous functions. However, there isn't any one "unified" way of emitting/catching events accross threads, and synchronization primitives are not themselves thread-safe. This can lead to unexpected behavior when trying to synchronize multiple event loops on multiple threads.
Solution
aioevt - After creating the manager, you can emit or await 'global' events in a thread-safe way. Callbacks can registered from any thread and target any event loop. This allows you to very easily share objects and quickly emit information without fussing with thread safety.
Documentation
Evt and EvtData
The core objects used throughout aioevt are the Evt and EvtData dataclassess.
Evtrepresents an event itself and is comprised of aname(identifier),func(callback),loop(for execution), andrecurring(automatic re-scheduling)EvtDataconsists only ofargsandkwargswhich are splatted into callbacks as needed
Create a manager
Create an aioevt manager which uses the default event loop.
mgr = aioevt.Manager()
Register an event
Register a global event to be triggered from a provided event loop when a named event is emitted. This can be done in two ways: both through the mgr.register method, or the mgr.on decorator. An event can have multiple callbacks, and each callback will be invoked with the same parameters on each emit. Note: The return value of the event callback is not retrievable. If you'd like to handle a value from inside a callback, simply emit a different event and wait for it in the desired location.
mgr.register(
name="MyEvent", # Name by which the event will be referenced
func=my_func, # Synchronous or Asynchronous function
loop=my_event_loop, # Provide a target loop in which to execute the function, Default: None (get running)
recurring=True, # Determines if the event should be re-registered after the first emit, Default: True
)
and
@mgr.on(name="Add", loop=my_event_loop, recurring=True)
def my_callback(num1, num2, num3, num4):
# e.g. run hard calculations within a ProcessPoolExecutor
total = num1 + num2 + num3 + num4
mgr.emit("Calculated", args=(total,))
mgr.emit_after(0.1, "Add", args=(1, 2, 3, 4))
data = await mgr.wait("Calculated")
assert data.args[0] == 10
Emitting an event
Emit a signal with arbitrary positional and/or keyword parameters. This can be done with mgr.emit or mgr.emit_after which is identical except that it accepts an additional delay argument as its first parameter.
mgr.emit(
name="MyEvent", # Name of the event to emit
args=(1, 2, 3), # Tuple of args used to emit
kwargs={"num4": 4}, # Dict of kwargs used to emit
)
Waiting for an event
Using mgr.wait, you can asynchronously wait until an event is fired. This is commonly used just to wait for a certain status, but will also return an EvtData object which contains the args and kwargs values that were passed into the call to mgr.emit
data = await mgr.wait(
name="MyEvent", # Name of the event to wait for
timeout=None, # Timeout in seconds, Default: None
)
print(data.args) # mgr.emit(..., args=...)
print(data.kwargs) # mgr.emit(..., kwargs=...)
Unregistering an event
Recurring events can be unregistered manually both by name and by function value. Note that unregistering by name is significantly faster and more efficient, so use that when possible.
mgr.unregister(name="MyEventName")
mgr.unregister(func=my_callback_func)
Metadata
Release files for aioevt 2.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| aioevt-2.2.1.tar.gz | 7.6 kB | Without provenance | Details for aioevt-2.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| aioevt-2.2.1-py3-none-any.whl | Python 3 | none | any | Details for aioevt-2.2.1-py3-none-any.whl |
Total release size: 28.6 kB
Release files / aioevt-2.2.1.tar.gz
| Download URL | aioevt-2.2.1.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4b5e5a327ba8b864263cef3613282457373bf7e531dd8ab56750cb73f5b4d5d6
|
|
BLAKE2b-256 checksum How to use checksums |
abadf70cfa258b6452afafec1e7ccd48935eb6c36d93bfd040cae35c3d443ad1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1
|
Release files / aioevt-2.2.1-py3-none-any.whl
| Download URL | aioevt-2.2.1-py3-none-any.whl |
|---|---|
| Size | 21.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6d72e19dd46a151307c167329a5c6a7fd80f7141711c36c7a6eecea76071441a
|
|
BLAKE2b-256 checksum How to use checksums |
ea799b382eabda210ee566daf374d8e2349b4b80765ad0fae9cea46cb265bbfd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1
|
Security for aioevt 2.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.