No project description provided
Project description
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.
Evt
represents an event itself and is comprised of aname
(identifier),func
(callback),loop
(for execution), andrecurring
(automatic re-scheduling)EvtData
consists only ofargs
andkwargs
which 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)
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
Built Distribution
File details
Details for the file aioevt-2.2.0.tar.gz
.
File metadata
- Download URL: aioevt-2.2.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b725cc63a28c88ab4053aa535cdcc495dff2ff388033bd50bbab13ffbe4a366 |
|
MD5 | 572499fad6a331c382a5e4bc49f281bf |
|
BLAKE2b-256 | fa27f5b85d655e165c6d6c962dcaff77c96123d64ffcc884a000ecb6d09e82dd |
File details
Details for the file aioevt-2.2.0-py3-none-any.whl
.
File metadata
- Download URL: aioevt-2.2.0-py3-none-any.whl
- Upload date:
- Size: 20.9 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1529b94eb3e90316acd3efc3eae0234ae79e15e8216f566f9e486ead88ca858a |
|
MD5 | 5a14484cbcd1f32ad66f4326426233ea |
|
BLAKE2b-256 | 6302da0e1e57321e17aae782c06e2e552ac3d52250068cbe662ab46e2699b2d7 |