同时支持的同步和异步消息通知的eventbus,灵感来自与google guava eventbus
用法: 1.定义接收消息类型:
class IntMessage(int):
pass
class DictMessage(dict):
pass
2.定义需要接收消息的Observer类,需继承subscriber.DefaultSubscriber, 并实现subscribe方法,subscribe方法的参数类型与自定义的消息类型保持一致
class CConcreteObserver(DefaultSubscriber):
def subscribe(self, msg: msg.IntMessage):
time.sleep(2)
print(f"{self.__class__.__name__}#{inspect.stack()[0][3]}: msg:{msg}")
3.根据消息类型,添加接收者
OBSERVER_REGISTRY = [
{
msg_type(msg.DictMessage()): [
subscriber.AConcreteObserver()
]
},
{
msg_type(msg.IntMessage()): [
subscriber.BConcreteObserver(),
subscriber.CConcreteObserver(),
]
},
]
4.在需要的地方引入eventbus,根据自己的需求选择支持同步的方式(eventbus.EventBus)或异步的方式(eventbus.AsyncEventBus) 在消息接收处,调用notify接口发送消息给对应的所有观察者
sync_event_bus = eventbus.AsyncEventBus()
sync_event_bus.notify(msg.IntMessage(1211))
Metadata
Release files for pyeventbus2 1.0.0
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 | |
|---|---|---|---|---|
| pyeventbus2-1.0.0.tar.gz | 6.9 kB | Without provenance | Details |
Release files / pyeventbus2-1.0.0.tar.gz
| Download URL | pyeventbus2-1.0.0.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0461eaa5e2948e9d0ff5ccf51ed5d668a0716d7b4e809d7e92851f97e476e003
|
|
BLAKE2b-256 checksum How to use checksums |
8e713f4043f359bf0eb859305589103e3ac6f5fddf3a8dff7948be808de8c3f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7
|
Security for pyeventbus2 1.0.0
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.