实现发布者/订阅者模式的消息服务
apubsub的Python项目详细描述
阿普苏布
简单、单一用途的消息服务实现。在
安装
Python versin 3.7+必需
只需使用pip安装它:pip install apubsub
用法
fromapubsubimportServiceservice=Service()# Note that service is started in stand-alone process# so start it as early as possible to minimize resource pickling*service.start()classKlass:def__init__(self):self.sub=service.get_client()awaitself.sub.start_consuming()# subscriber should be startedself.pub=service.get_client()# if used only as publisher, it is not requiredasyncdefdo_smth(self):data=awaitself.sub.get(.1)# fetch received data with timeoutifdataisNone:print("No data received by subscriber")returnprint(data)asyncdefdo_smth_else(self):msg="some string data"awaitself.pub.publish("topic",msg)asyncdefuse_iter_get(self):asyncfordatainself.sub.get_iter():print(f"Data received: {data}")
查看更多测试示例
- 项目
标签: