Pithonistas™的Zeromq

zeroless的Python项目详细描述


Build StatusCoverage StatusCodacyPyPiDocsLicense

又一个python的包装器。但是,与PyZMQ不同,后者 试图与C++实现非常接近,这个项目的目的是 使使用ØMQ的分布式系统尽可能像Python一样。

由于使用更简单,零不支持所有好的方面 以及ØMQ的特征。但是,您可以期望找到所有消息 传递您习惯的模式(即配对、请求/回复, 发布者/订阅者,推/拉)。唯一的交通工具 可用的是tcp,因为在python中由于 gil和ipc仅限于unix。

安装

$ pip install zeroless

python api

zeroless模块中,可以使用两个类来定义如何分布 实体是相关的(即ServerClient)。直截了当地说, 除了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/

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
playframework Play Framework 2.5表单对象bean(Java)中的依赖项注入   java从javascript触发小程序按钮   JavaGWT:如何创建我自己的异步方法?   对于JTA事务类型,java JPA更改不会持久化   java如何使用预定义的甲板类制作Black Jack游戏   带有数组的java HashMap find()   从Java中的另一个类接收对象数组的参数?   java IntelliJ错误:Scala 145,错误:scalac:找不到Scala编译器JAR   java Eclipse 404请求的资源不可用   java Chuck没有界限   java阅读行每隔一行跳过一行   java如何访问我的EAR部署信息   Java中的正则表达式,其中执行搜索的文本是动态变化的   java我需要优化BigInteger的toString()方法   java Google Map Api V2,自定义路线标记   java如何在Java8中与LocalDate实现更少的需求和更高的质量   EclipseUML插件生成Java源代码   将数据库列绑定到java jComboBox和JList   java如何将两个图像合并为PDF