基于异步的分层网络库,提供请求-应答通道、rpc和多代理系统。
aiomas的Python项目详细描述
aioma–一个用于多代理系统和基于异步的rpc的库
aiomas是一个易于使用的库,用于request-reply channels,remote 过程调用(rpc)和multi-agent systems(mas)。是用纯文字写的 Python在asyncio上面。
下面是三个简单的例子,展示了aioma的不同层次和 它们相互补充的内容:
request-reply通道的抽象级别最低(但已经 提供比普通异步更多的功能:
>>>importaiomas>>>>>>>>>asyncdefhandle_client(channel):..."""Handle a client connection."""...req=awaitchannel.recv()...print(req.content)...awaitreq.reply('cya')...awaitchannel.close()>>>>>>>>>asyncdefclient():..."""Client coroutine: Send a greeting to the server and wait for a ... reply."""...channel=awaitaiomas.channel.open_connection(('localhost',5555))...rep=awaitchannel.send('ohai')...print(rep)...awaitchannel.close()>>>>>>>>>server=aiomas.run(aiomas.channel.start_server(('localhost',5555),handle_client))>>>aiomas.run(client())ohaicya>>>server.close()>>>aiomas.run(server.wait_closed())
rpc层在其上添加远程过程调用:
>>>importaiomas>>>>>>>>>classMathServer:...router=aiomas.rpc.Service()......@router.expose...defadd(self,a,b):...returna+b...>>>>>>asyncdefclient():..."""Client coroutine: Call the server's "add()" method."""...rpc_con=awaitaiomas.rpc.open_connection(('localhost',5555))...rep=awaitrpc_con.remote.add(3,4)...print('What’s 3 + 4?',rep)...awaitrpc_con.close()>>>>>>server=aiomas.run(aiomas.rpc.start_server(('localhost',5555),MathServer()))>>>aiomas.run(client())What’s3+4?7>>>server.close()>>>aiomas.run(server.wait_closed())
最后,agent层隐藏了设置所需的一些样板代码 套接字并允许代理实例轻松地相互通信:
>>>importaiomas>>>>>>classTestAgent(aiomas.Agent):...def__init__(self,container):...super().__init__(container)...print('Ohai, I am %s'%self)......asyncdefrun(self,addr):...remote_agent=awaitself.container.connect(addr)...ret=awaitremote_agent.service(42)...print('%s got %s from %s'%(self,ret,remote_agent))......@aiomas.expose...defservice(self,value):...returnvalue>>>>>>c=aiomas.Container.create(('localhost',5555))>>>agents=[TestAgent(c)foriinrange(2)]Ohai,IamTestAgent('tcp://localhost:5555/0')Ohai,IamTestAgent('tcp://localhost:5555/1')>>>aiomas.run(until=agents[0].run(agents[1].addr))TestAgent('tcp://localhost:5555/0')got42fromTestAgentProxy('tcp://localhost:5555/1')>>>c.shutdown()
aiomas根据mit许可证发布。它需要Python3.4及更高版本 在Linux、OS X和Windows上运行。
安装
aiomas需要python>;=3.6(或pypy3>;=5.10.0)。它使用json编解码器 默认情况下,只有纯python依赖项。
通过pip安装aiomas,方法是运行:
$ pip install aiomas
您可以启用可选的MsgPack编解码器或其Blosc压缩版本 通过安装相应的特性(注意,您需要一个C编译器 安装它们):
$ pip install aiomas[mp]# Enables the MsgPack codec $ pip install aiomas[mpb]# Enables the MsgPack and MsgPackBlosc codecs
功能
aiomas只在原始tcp/unix域周围放置三层抽象 由asyncio提供的套接字:
- 代理和代理容器:
顶层为您自己的代理提供了一个简单的基类。所有代理人 住在集装箱里。
容器负责创建代理实例并执行 他们之间的交流。
容器为代理提供一个clock。这个钟可以是 与实际(挂钟)时间同步或由外部进程设置 (例如,其他模拟器)。
- rpc:
层rpc实现远程过程调用,允许您调用方法 在远程对象上,就像它们是普通对象一样:
你写的不是ret = obj.meth(arg),而是ret = await obj.meth(arg)。
- 请求回复频道:
- 层是rpc层的基础。它发送JSON或 MsgPackTCP或Unix域套接字上的编码字节字符串。它还映射 回复(成功或失败的)他们相应的请求。
其他功能:
- TLS支持授权和加密通信。
- 可互换和可扩展的编解码器:json和msgpack(后者 (可选地使用blosc压缩)是内置的。您可以添加自定义编解码器或 为自己的对象编写(反)序列化程序以扩展编解码器。
- 确定性的、模拟的套接字:一个localqueue传输允许您发送和 在一个单一的 过程。这有助于测试和调试分布式算法。
计划的功能
关于未来版本的一些想法:
- 可选的连接丢失后自动重新连接
贡献
使用以下设置开发环境:
$ virtualenv -p `which python3` aiomas $ pip install -r requirements-setup.txt
使用以下命令运行测试:
$ pytest
$ # or
$ tox
支持
许可证
这个项目是根据麻省理工学院的许可证授权的。
更改日志
2.0.1–2017年12月29日
- [更改]恢复对Python3.5的支持,以便文档在读取文档时 再造一次。
2个0.0–2017年12月28日
- [中断]转换为f字符串和async/await语法。这个 最低要求的python版本现在是python 3.6和pypy3 5.10.0。
- [中断]删除了aiomas.util.async()和aiomas.util.create_task()。
- [更改]从Bitbucket和Mercurial移动到Gitlab和Git。
- [修复]调整到异步更改并显式地将引用传递到当前 必要时进行事件循环。
您可以在documentation中找到有关旧版本的信息。