跳转到主要内容

异步Thrift服务器和客户端

项目描述

基于 thriftpy2 的 thrift 协议的 asyncio 实现。

https://travis-ci.org/ryanwang520/aiothrift.svg?branch=master

文档: https://aiothrift.readthedocs.org/

安装

$ pip install aiothrift

使用示例

Thrift 文件

service PingPong {
    string ping(),
    i64 add(1:i32 a, 2:i64 b),
}

服务器

import asyncio
import aiothrift

pingpong_thrift = aiothrift.load('pingpong.thrift', module_name='pingpong_thrift')

class Dispatcher:
    def ping(self):
        return "pong"

    async def add(self, a, b):
        await asyncio.sleep(1)
        return a + b

async def main():
  server = await aiothrift.create_server(pingpong_thrift.PingPong, Dispatcher()))
  async with server:
      await server.serve_forever()

asyncio.run(main())

客户端

import asyncio
import aiothrift

pingpong_thrift = aiothrift.load('pingpong.thrift', module_name='pingpong_thrift')

async def go():
    conn = await aiothrift.create_connection(pingpong_thrift.PingPong)
    print(await conn.ping())
    print(await conn.add(5, 6))
    conn.close()

asyncio.run(go())

或使用 ConnectionPool

import asyncio
import aiothrift

pingpong_thrift = aiothrift.load('pingpong.thrift', module_name='pingpong_thrift')

async def go():
    client = await aiothrift.create_pool(pingpong_thrift.PingPong)
    print(await client.ping())
    print(await client.add(5, 6))
    client.close()
    await client.wait_closed()

asyncio.run(go())

开始使用 aiothrift 就这么简单,您不必在服务器和客户端双方都使用 aiothrift。所以,如果您已经有一个正常的 thrift 服务器设置,可以自由地创建一个异步 thrift 客户端来与该服务器通信。

需求

LICENSE

aiothrift 基于 MIT 许可协议提供。

项目详情


下载文件

下载您平台对应的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

aiothrift-0.2.7.tar.gz (12.6 kB 查看哈希值)

上传时间

构建分布

aiothrift-0.2.7-py3-none-any.whl (14.0 kB 查看哈希值)

上传时间 Python 3

由以下支持