Pithonistas™的Zeromq
zeroless的Python项目详细描述
由于使用更简单,零不支持所有好的方面 以及ØMQ的特征。但是,您可以期望找到所有消息 传递您习惯的模式(即配对、请求/回复, 发布者/订阅者,推/拉)。唯一的交通工具 可用的是tcp,因为在python中由于 gil和ipc仅限于unix。
安装
$ pip install zeroless
python api
在zeroless模块中,可以使用两个类来定义如何分布 实体是相关的(即Server和Client)。直截了当地说, 除了pair模式,一个客户机可以连接到多个 服务器,而服务器可以接受来自多个客户端的传入连接。
服务器和客户端都可以创建callable和/或iterable, 取决于消息传递模式。以便您可以在传入的 信息和/或呼叫以发送信息。
所有示例假设:
fromzerolessimport(Server,Client)
推拉
用于在一组工人之间分配工作负载。普通人 流处理领域的模式,是 例如apache storm之类的应用程序。而且,它可以被看作 地图缩减模式的概括。
# Binds the pull server to port 12345# And assigns an iterable to wait for incoming messageslisten_for_push=Server(port=12345).pull()formsginlisten_for_push:print(msg)
# Connects the client to as many servers as desiredclient=Client()client.connect_local(port=12345)# Initiate a push client# And assigns a callable to push messagespush=client.push()formsgin[b"Msg1",b"Msg2",b"Msg3"]:push(msg)
发布者订阅服务器
用于向一组对等方广播消息。一种常见的模式 在客户端允许实时通知,而不必 采用一些不成熟的方法,比如共用。在线服务,如 pubnub或像mqtt这样的物联网协议就是这种模式使用的例子。
# Binds the publisher server to port 12345# And assigns a callable to publish messages with the topic 'sh'pub=Server(port=12345).pub(topic=b'sh',embed_topic=True)# Gives publisher some time to get initial subscriptionssleep(1)formsgin[b"Msg1",b"Msg2",b"Msg3"]:pub(msg)
# Connects the client to as many servers as desiredclient=Client()client.connect_local(port=12345)# Initiate a subscriber client# Assigns an iterable to wait for incoming messages with the topic 'sh'listen_for_pub=client.sub(topics=[b'sh'])fortopic,msginlisten_for_pub:print(topic,' - ',msg)
注意:自zmq 3.0以来,zmq的主题过滤功能是发布者端的。
最后但并非最不重要的是,绑定的子套接字在 首先通过提供的生成器请求,因此如果 丢失一些消息不是一个选项。
请求回复
对于rpc样式的调用很有用。客户机请求数据的通用模式 并接收与请求相关联的响应。http协议是 以采用这种模式而闻名,因为它对于restful是必不可少的 服务。
# Binds the reply server to port 12345# And assigns a callable and an iterable# To both transmit and wait for incoming messagesreply,listen_for_request=Server(port=12345).reply()formsginlisten_for_request:print(msg)reply(msg)
# Connects the client to as many servers as desiredclient=Client()client.connect_local(port=12345)# Initiate a request client# And assigns a callable and an iterable# To both transmit and wait for incoming messagesrequest,listen_for_reply=client.request()formsgin[b"Msg1",b"Msg2",b"Msg3"]:request(msg)response=next(listen_for_reply)print(response)
配对
通常,这种模式是不必要的,正如上面的那些 或者它们的混合足以满足分布式计算中的大多数用例。 就其功能而言,这种模式是最相似的替代方案 到上述模式中的常用posix套接字。因此, 期望一对一和双向通信。
# Binds the pair server to port 12345# And assigns a callable and an iterable# To both transmit and wait for incoming messagespair,listen_for_pair=Server(port=12345).pair()formsginlisten_for_pair:print(msg)pair(msg)
# Connects the client to a single serverclient=Client()client.connect_local(port=12345)# Initiate a pair client# And assigns a callable and an iterable# To both transmit and wait for incoming messagespair,listen_for_pair=client.pair()formsgin[b"Msg1",b"Msg2",b"Msg3"]:pair(msg)response=next(listen_for_pair)print(response)
日志记录
模块zeroless允许通过全局Logger object进行日志记录。
fromzerolessimportlog
要启用它,只需添加一个Handler object并设置适当的logging level。
测试
运行单个测试:
$ py.test tests/test_desired_module.py
运行所有测试:
$ python setup.py test
或者,您可以使用tox:
$ tox
有关更多信息,请参见我们的documentation。
许可证
版权所有2014 Lucas Lira Gomesx8lucas8x@gmail.com
这个库是免费软件;您可以重新分发和/或修改它 根据由 自由软件基金会;许可证的2.1版,或 你的选择)任何更高版本。
这个图书馆的发行是希望它会有用,但是 没有任何保证;甚至没有ED保证 适销性或适合某一特定目的的适销性。看小GNU 一般公共许可证了解更多详细信息。
你应该收到GNU Lesser通用公共许可证的副本 还有这个图书馆。如果没有,请参阅http://www.gnu.org/licenses/。