Examples of aiothrift usage¶
aio thrift server¶
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(2)
return a + b
async def main():
server = await aiothrift.create_server(pingpong_thrift.PingPong, Dispatcher())
async with server:
print("server is listening on host {} and port {}".format("127.0.0.1", 6000))
await server.serve_forever()
if __name__ == "__main__":
asyncio.run(main())
aio thrift client¶
import asyncio
import aiothrift
pingpong_thrift = aiothrift.load("pingpong.thrift", module_name="pingpong_thrift")
async def create_connection():
conn = await aiothrift.create_connection(
pingpong_thrift.PingPong, ("127.0.0.1", 6000), timeout=10
)
print(await conn.ping())
print(await conn.add(5, 6))
conn.close()
asyncio.run(create_connection())
connection pool sample¶
import aiothrift
import asyncio
pingpong_thrift = aiothrift.load("pingpong.thrift", module_name="pingpong_thrift")
async def create_pool():
return await aiothrift.create_pool(
pingpong_thrift.PingPong, ("127.0.0.1", 6000), timeout=3
)
async def main(pool):
print(await pool.ping())
print(await pool.add(5, 6))
print(await pool.ping())
if __name__ == "__main__":
async def f():
pool = await create_pool()
await asyncio.gather(main(pool), main(pool))
pool.close()
await pool.wait_closed()
asyncio.run(f())