Giter Club home page Giter Club logo

aiothrift's Introduction

aiothrift

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

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

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 create_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(
    create_server(pingpong_thrift.PingPong, Dispatcher(), 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, loop=loop)
    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, loop=loop)
    async with pool.get() as conn:
        print(await conn.ping())
        print(await conn.add(5, 6))
    pool.close()
    await 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.

aiothrift's People

Contributors

ryanwang520 avatar superisaac avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.