MrEventLoop
Simple event system for Python building on asyncio and working seamlessly across sockets.
Installation
pip install mreventloop
Emitting Events
from mreventloop import emits
@emits('events', [ 'produced' ])
class Producer:
def requestProduct(self):
self.events.produced('some product')
You can now connect a callback to the event:
from mreventloop import connect
producer = Producer()
connect(producer, 'produced', lambda product: print(f'{product}'))
You can disconnect all listeners from an event:
from mreventloop import disconnect
disconnect(producer, 'produced')
Event Loop and Slots
Objects can have an event loop building on asyncio:
from mreventloop import has_event_loop, slot
@has_event_loop('event_loop')
class Consumer:
@slot
def onProduced(self, product):
print(f'{product}')
Calling a @slot will not run the corresponding method directly,
but queue its execution on the object's event loop.
The event loop will run them strictly sequentially in FIFO order.
Thus, slots do not need to be reentrant.
The event loop has to be started:
consumer = Consumer()
connect(producer, 'produced', consumer, 'onProduced')
async with consumer.event_loop:
producer.requestProduct()
After exiting, the event loop will still process all queued events.
When creating an object that has_event_loop,
an event loop will automatically be created.
But you can also share event loops between objects:
producer1 = Producer()
producer2 = Producer()
consumer1 = Consumer()
consumer2 = Consumer()
consumer2.event_loop = consumer1.event_loop
connect(producer1, 'produced', consumer1, 'onProduced')
connect(producer2, 'produced', consumer2, 'onProduced')
async with consumer1.event_loop:
producer1.requestProduct()
producer2.requestProduct()
All calls to slots in both consumer1 and consumer2 will be run sequentially.
Async Slots
Slots can also be coroutines:
import asyncio
@has_event_loop('event_loop')
class ConsumerAsync:
@slot
async def onProduced(self, product):
await asyncio.sleep(0.1)
print(f'{product}')
The coroutine will be awaited inside the event loop.
Awaiting Events
Events can be awaited:
from mreventloop import SyncEvent
producer = Producer()
sync_event = SyncEvent(producer.events.produced)
async def emit():
producer.requestProduct()
asyncio.create_task(emit())
result = await sync_event
print(f'{result}')
Events of the Event Loop
The event loop itself emits the following events:
[ 'started', 'stopped', 'active', 'idle', 'exception' ]
Exceptions on the Event Loop
If an exception ocurrs executing a slot, the app will exit. This can be prevented by creating the loop manually with:
from mreventloop import EventLoop
consumer.event_loop = EventLoop(exit_on_exception = False)
In this case, any exception will be emitted through the exception event.
(The general idea here is to just not use exceptions.)
Crossing Sockets
Events and slots across multiple applications can be connected via sockets.
This requires one instance of a Broker and any number of Peers.
A Peer subscribes to a number of events.
If any other Peer publishes such events,
they will be emitted by the subscribing Peer.
from mreventloop import Broker, Peer
in_socket_path = 'ipc:///tmp/mreventloop_test_in.sock'
out_socket_path = 'ipc:///tmp/mreventloop_test_out.sock'
producer_peer = Peer(in_socket_path, out_socket_path, [ 'request_product' ], [ 'produced' ])
consumer_peer = Peer(in_socket_path, out_socket_path, [ 'produced' ], [ 'request_product' ])
async with Broker(in_socket_path, out_socket_path), producer_peer, consumer_peer:
consumer = Consumer()
connect(consumer, 'request_product', consumer_peer.publish, 'request_product')
connect(consumer_peer, 'product', consumer, 'onProduct')
producer = Producer()
connect(producer, 'produce', producer_peer.publish, 'produce')
connect(producer_peer, 'request_product', producer, 'onRequestProduct')
async with producer.event_loop, consumer.event_loop:
consumer.events.request_product()
Metadata
Release files for MrEventLoop 0.7.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 |
|---|---|---|---|---|
| MrEventLoop-0.7.1.tar.gz | 15.0 kB | Without provenance | Details for MrEventLoop-0.7.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| MrEventLoop-0.7.1-py3-none-any.whl | Python 3 | none | any | Details for MrEventLoop-0.7.1-py3-none-any.whl |
Total release size: 33.4 kB
Release files / MrEventLoop-0.7.1.tar.gz
| Download URL | MrEventLoop-0.7.1.tar.gz |
|---|---|
| Size | 15.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba29f4848676e4d20a0196f1db241fd2c6665c8fc4b1d028d16801ed8a1688ab
|
|
BLAKE2b-256 checksum How to use checksums |
7aad4b4368923296ee68c524e1e9fac46022421b200a9c684bb734275517a6d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Release files / MrEventLoop-0.7.1-py3-none-any.whl
| Download URL | MrEventLoop-0.7.1-py3-none-any.whl |
|---|---|
| Size | 18.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4048f76b36776a657911c8d78d3fd660d6abc12fa2d54126998125ddaf855be3
|
|
BLAKE2b-256 checksum How to use checksums |
32420bbd4932f001ee05d336603489fe0af6124559a8468f6b0d6c679960b8af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Security for MrEventLoop 0.7.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.