Skip to main content

async thrift server and client

Project description

Asyncio implementation for thrift protocol, which is heavily based on thriftpy.

http://img.shields.io/travis/moonshadow/aiothrift/master.svg?style=flat

This project is still in early develop state and thus is not recommended for production usage.

Documentation: https://aiothrift.readthedocs.org/

Installation

$ pip install aiothrift

Usage example

Thrift file

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

Server

import asyncio
import thriftpy
from aiothrift.server import make_server

pingpong_thrift = thriftpy.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

loop = asyncio.get_event_loop()
server = loop.run_until_complete(
    make_server(pingpong_thrift.PingPong, Dispatcher(), ('127.0.0.1', 6000), loop=loop))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

Client

import thriftpy
import asyncio
import aiothrift

loop = asyncio.get_event_loop()
pingpong_thrift = thriftpy.load('pingpong.thrift', module_name='pingpong_thrift')

async def go():
    conn = await aiothrift.create_connection(pingpong_thrift.PingPong, ('127.0.0.1', 6000), loop=loop, timeout=2)
    print(await conn.ping())
    print(await conn.add(5, 6))
    conn.close()

loop.run_until_complete(go())
loop.close()

Or use ConnectionPool

import thriftpy
import asyncio
import aiothrift

loop = asyncio.get_event_loop()
pingpong_thrift = thriftpy.load('pingpong.thrift', module_name='pingpong_thrift')

async def go():
    pool = await aiothrift.create_pool(pingpong_thrift.PingPong,
        ('127.0.0.1', 6000), loop=loop, timeout=2)
    async with pool.get() as conn:
        print(await conn.ping())
        print(await conn.add(5, 6))
    pool.close()
    yield from pool.wait_closed()

loop.run_until_complete(go())
loop.close()

It’s just that simple to begin with aiothrift, and you are not forced to use aiothrift on both server and client side. So if you already have a normal thrift server setup, feel free to create an async thrift client to communicate with that server.

Requirements

LICENSE

aiothrift is offered under the MIT license.

Archer Changelog

Here you can see the full list of changes between each aiothrift release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiothrift-0.0.7.tar.gz (12.5 kB view details)

Uploaded Source

File details

Details for the file aiothrift-0.0.7.tar.gz.

File metadata

  • Download URL: aiothrift-0.0.7.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aiothrift-0.0.7.tar.gz
Algorithm Hash digest
SHA256 63ef5b2b629530d56e5cacd6920940642fa855524d614d1ae69c6c27f4b543b6
MD5 5224f3d9da165578fcb4872777f753e8
BLAKE2b-256 b2f08244e4b93f1177fb80aedd3ee77e50d86bc2f94746f4057b6cd2f53f5a1f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page