-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added documentation and trying to fix the test.yml.
- Loading branch information
1 parent
0260656
commit f89130f
Showing
5 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,56 @@ | ||
# Distributed EventBus & DEntryPoint | ||
|
||
For the distributed eventbus implementation, the data transmitted within the bus needs to be serializable. | ||
|
||
!!! note | ||
Make sure to use ``DataClassJsonMixin`` when using a ``dataclass`` to permit the serialization of the data and documenting the dtypes. | ||
|
||
```python | ||
from dataclasses import dataclass | ||
from dataclasses_json import DataClassJsonMixin | ||
from aiodistbus import DEntryPoint, DEventBus | ||
``` | ||
|
||
The handlers for events is the same as the local eventbus implementation, making interoperability feasible. | ||
|
||
```python | ||
@dataclass | ||
class ExampleEvent(DataClassJsonMixin): | ||
msg: str | ||
|
||
async def handler(event: ExampleEvent): | ||
assert isinstance(event, ExampleEvent) | ||
logger.info(f"Received event {event}") | ||
``` | ||
|
||
After the configuration, we have to create the necessary server-client resources and connect the setup: | ||
|
||
```python | ||
# Create resources | ||
dbus = DEventBus() | ||
e1, e2 = DEntryPoint(), DEntryPoint() | ||
|
||
# Add handlers | ||
await e1.on('example', handler, ExampleEvent) | ||
|
||
# Connect | ||
await e1.connect(dbus.ip, dbus.port) | ||
await e2.connect(dbus.ip, dbus.port) | ||
``` | ||
|
||
With everything configured, we can not emit messages between the entrypoints: | ||
|
||
```python | ||
# Send message and e1's handler is executed | ||
event = await e2.emit('example', ExampleEvent(msg="hello")) | ||
``` | ||
|
||
Make sure to close the resources at the end of the program. | ||
|
||
|
||
```python | ||
# Closing (order doesn't matter) | ||
await bus.close() | ||
await e1.close() | ||
await e2.close() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Registry: Event Handler Decorator | ||
|
||
To link the handlers to event types in a more streamline fashion, we can use a ``Registry`` to have ``Namespaces`` decided to list of handlers and their event types. | ||
|
||
Here we perform the imports and configuration with the registry | ||
|
||
```python | ||
from aiodistbus import Event, registry | ||
|
||
@dataclass | ||
class ExampleEvent: | ||
msg: str | ||
|
||
@registry.on("test", ExampleEvent) | ||
async def func(event: ExampleEvent): | ||
assert isinstance(event, ExampleEvent) | ||
print(event) | ||
``` | ||
|
||
Then we create the resources and leverage the ``use`` method of the ``EntryPoint`` to access the stored handler/event_type pairs. | ||
|
||
```python | ||
# Create resources | ||
e1, e2 = entrypoints | ||
|
||
# Add handlers | ||
await e1.use(registry) | ||
|
||
# Connect | ||
await e1.connect(bus) | ||
await e2.connect(bus) | ||
|
||
# Send message | ||
event = await e2.emit(event_type, dtype_instance) | ||
``` | ||
|
||
The reason for a registry instead of a ``@ee.on('test', handler)``, like ``pyee``'s ``EventEmitter``, aproach is because in a multiprocessing context, a global eventbus or entrypoint isn't not going to work properly accross different multiprocessing context -- especially Windows's "spawn". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters