Skip to content

Commit c028db1

Browse files
Feature/bus registration (#14)
* Add methods to connect buses. * Use sets instead of lists for internal storage of collections. * Add tests and documentation.
1 parent d526288 commit c028db1

File tree

9 files changed

+408
-265
lines changed

9 files changed

+408
-265
lines changed

.flake8

Lines changed: 0 additions & 9 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ on:
66
branches:
77
- "*"
88
jobs:
9+
code-style:
10+
name: code-style
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Set up Python 3.10
14+
uses: actions/setup-python@v4
15+
with:
16+
python-version: "3.10"
17+
- name: install poetry
18+
run: |
19+
curl -sSL https://install.python-poetry.org | python - -y
20+
- name: checkout code
21+
uses: actions/checkout@v2
22+
- name: install package
23+
run: poetry install
24+
- name: ruff check
25+
run: poetry run ruff check
26+
- name: ruff format
27+
run: poetry run ruff format --check
928
unit-test:
1029
name: unit test
1130
runs-on: ubuntu-latest
@@ -21,11 +40,9 @@ jobs:
2140
uses: actions/checkout@v2
2241
- name: install package
2342
run: poetry install
43+
- name: ruff check
44+
run: poetry run ruff check
45+
- name: ruff format
46+
run: poetry run ruff format --check
2447
- name: pytest
25-
run: poetry run pytest
26-
- name: black
27-
run: poetry run black --check .
28-
- name: isort
29-
run: poetry run isort -c .
30-
- name: flake8
31-
run: poetry run flake8
48+
run: poetry run pytest

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,51 @@ async def main():
144144
async with BUS:
145145
await asyncio.sleep(5)
146146
asyncio.run(main())
147-
```
147+
```
148+
149+
## Buses Across Multiple Modules
150+
Most practical use of asyncio-signal-bus involve publishers and subscribers across
151+
multiple modules or files. In order to connect buses across modules we make a bus in
152+
each file.
153+
154+
```python
155+
# my_app/module_1.py
156+
from asyncio_signal_bus import SignalBus
157+
158+
BUS_1 = SignalBus()
159+
160+
@BUS_1.publisher(topic_name="foo")
161+
async def foo_publisher(arg: str):
162+
signal = {"message": arg}
163+
return signal
164+
```
165+
166+
```python
167+
# my_app/module_2.py
168+
from asyncio_signal_bus import SignalBus
169+
import json
170+
171+
BUS_2 = SignalBus()
172+
173+
@BUS_2.subscriber(topic_name="foo")
174+
async def foo_subscriber_0(signal: dict):
175+
print(f"foo subscriber 0 received {json.dumps(signal)}")
176+
```
177+
178+
Now we connect all buses in a parent bus. All buses will now function as a single bus.
179+
180+
```python
181+
# my_app/__main__.py
182+
import asyncio
183+
from asyncio_signal_bus import SignalBus
184+
from my_app.module_1 import BUS_1, foo_publisher
185+
from my_app.module_2 import BUS_2
186+
187+
PARENT_BUS = SignalBus()
188+
PARENT_BUS.connect(BUS_1, BUS_2)
189+
190+
async def main():
191+
inputs = [f"message:{i}" for i in range(10)]
192+
async with PARENT_BUS:
193+
await asyncio.gather(*[foo_publisher(x) for x in inputs])
194+
```

0 commit comments

Comments
 (0)