eventhub
EventHub provides a lightweight event management system for Python projects using a simple publish/subscribe pattern.
Installation
pip install eventhub
NOTE: This example requires redis or a redis compatible server (like DragonflyDB) to be running on localhost:6379.
Usage Example
For a simple example, consider the following code:
pub.py:
import uuid
import asyncio
from eventhub import EventHub, Event
from redis.asyncio import Redis
r = Redis(host='localhost', port=6379, db=0)
# PUBLISH/SUBSCRIBE example (one-way communication)
async def main():
hub = EventHub(r)
for i in range(10):
# for every other event, publish to a different topic
if i % 2 == 0:
event = Event(topic="foo", payload={"id": str(uuid.uuid4())})
else:
event = Event(topic="bar", payload={"id": str(uuid.uuid4())})
await hub.publish(event)
await hub.close()
if __name__ == "__main__":
asyncio.run(main())
sub.py:
import asyncio
from eventhub import EventHub, Event
from redis.asyncio import Redis
r = Redis(host='localhost', port=6379, db=0)
hub = EventHub(r)
@hub.subscribe(topic="foo")
async def handler_foo(event: Event):
print("got foo event", event)
@hub.subscribe(topic="foo")
async def handler_foo2(event: Event):
print("got foo event (2)", event)
@hub.subscribe(topic="bar")
async def handler_bar(event: Event):
print("got bar event", event)
async def main():
await hub.start()
async def shutdown():
print("Shutting down EventHub...")
await hub.shutdown()
print("EventHub shutdown complete.")
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
# Instead of directly running hub.shutdown() in another asyncio.run,
# we ensure that the shutdown process completes before closing the loop.
loop = asyncio.get_event_loop()
loop.run_until_complete(shutdown())
loop.close()
example output:
got foo event topic='foo' payload={'id': '44f22b8b-0cef-427f-9bbe-2e6b4d43803a'}
got foo event (2) topic='foo' payload={'id': '44f22b8b-0cef-427f-9bbe-2e6b4d43803a'}
got bar event topic='bar' payload={'id': '9c8ec0bd-209b-40cb-bc04-66ee6bd4c82c'}
got foo event topic='foo' payload={'id': '8684450c-1382-46ba-8559-1a3791491973'}
got foo event (2) topic='foo' payload={'id': '8684450c-1382-46ba-8559-1a3791491973'}
got bar event topic='bar' payload={'id': 'be887cd5-bd35-4587-a138-b85c10a68fab'}
got foo event topic='foo' payload={'id': '619a9d16-99b1-46a2-9108-7fa114c03d8c'}
got foo event (2) topic='foo' payload={'id': '619a9d16-99b1-46a2-9108-7fa114c03d8c'}
got bar event topic='bar' payload={'id': 'e1ffeb07-073d-42a5-af1f-8c3032d053b9'}
got foo event topic='foo' payload={'id': '6124ecd0-e7e0-4754-bbef-43b6f6a610d2'}
got foo event (2) topic='foo' payload={'id': '6124ecd0-e7e0-4754-bbef-43b6f6a610d2'}
got bar event topic='bar' payload={'id': '80b2e02e-b8d4-4f9b-8821-089a69dcdf9c'}
got foo event topic='foo' payload={'id': 'c0ddd161-9a14-4c4d-9369-2948cb75eec9'}
got foo event (2) topic='foo' payload={'id': 'c0ddd161-9a14-4c4d-9369-2948cb75eec9'}
got bar event topic='bar' payload={'id': '8904d68e-9213-4376-92c5-d05fc438410a'}
Metadata
Release files for eventhub 0.0.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 |
|---|---|---|---|---|
| eventhub-0.0.1.tar.gz | 3.4 kB | Without provenance | Details for eventhub-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| eventhub-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for eventhub-0.0.1-py3-none-any.whl |
Total release size: 7.1 kB
Release files / eventhub-0.0.1.tar.gz
| Download URL | eventhub-0.0.1.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6c0ee33ff53f09def961367f32db5edd91a5ab02f696cb6982b3550f827f1938
|
|
BLAKE2b-256 checksum How to use checksums |
23261e3bfa88115a389814dada35aab55a482334b6502e068f9599d5360f45b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / eventhub-0.0.1-py3-none-any.whl
| Download URL | eventhub-0.0.1-py3-none-any.whl |
|---|---|
| Size | 3.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4a7c681ef97bfec4ad6dd83b1435b02d1a140311be1baa627fd628edf81ae3d8
|
|
BLAKE2b-256 checksum How to use checksums |
74ef26ece50aefed5693a8f438f60d1a6dee7ca8aa238b16f190e4b6e9997c42
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Security for eventhub 0.0.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.