This part of the documentation covers all the interfaces of aiothrift. For parts where aiothrift depends on external libraries, we document the most important right here and provide links to the canonical documentation.

ThriftConnection Object

class aiothrift.ThriftConnection(service, *, iprot, oprot, address, loop=None, timeout=None)[source]

Thrift Connection.


find out all apis defined in thrift service, and create corresponding method on the connection object, ignore it if some api name is conflicted with an existed attribute of the connection object, which you should call by using the execute() method.


A coroutine which receive response from the thrift server

execute(api, *args, **kwargs)[source]

Execute a rpc call by api name. This is function is a coroutine.

  • api – api name defined in thrift file
  • args – positional arguments passed to api function
  • kwargs – keyword arguments passed to api function

result of this rpc call


TimeoutError if this task has exceeded the timeout


ThriftAppError if thrift response is an exception defined in thrift.


ConnectionClosedError: if server has closed this connection.

ThriftConnection Pool

class aiothrift.ThriftPool(service, address, *, minsize, maxsize, loop=None, timeout=None)[source]

Thrift connection pool.


Acquires a connection from free pool.

Creates new connection if needed.


Clear pool connections.

Close and remove all free connections. this pattern is interesting


Close all free and in-progress connections and mark pool as closed.

fill_free(*, override_min)[source]

make sure at least self.minsize amount of connections in the pool if override_min is True, fill to the self.maxsize.


Current number of free connections.


Returns used connection back into pool.

When queue of free connections is full the connection will be dropped.


Current connection total num, acquiring connection num is counted


class aiothrift.TProtocol(trans, strict_read=True, strict_write=True, decode_response=True)[source]

Base class for thrift protocols, subclass should implement some of the protocol methods, currently we only have TBinaryProtocol implemented for you.

class aiothrift.TBinaryProtocol(trans, strict_read=True, strict_write=True, decode_response=True)[source]

Binary implementation of the Thrift protocol driver.


class aiothrift.TProcessor(service, handler)[source]

Base class for thrift rpc processor, which works on two streams.


class aiothrift.Server(processor, protocol_cls=<class 'aiothrift.protocol.TBinaryProtocol'>, timeout=None)[source]


class aiothrift.ThriftError[source]

Base Exception defined by aiothrift

class aiothrift.ConnectionClosedError[source]

Raised if connection to server was closed.

class aiothrift.PoolClosedError[source]

Raised when operating on a closed thrift connection pool

class aiothrift.ThriftAppError(type=0, message=None)[source]

Application level thrift exceptions.

Useful functions

aiothrift.create_server(service, handler, address=('', 6000), loop=None, protocol_cls=<class 'aiothrift.protocol.TBinaryProtocol'>, timeout=None)[source]

create a thrift server. This function is a coroutine.

  • service – thrift Service
  • handler – a dispatcher object which is a namespace for all thrift api functions.
  • address – (host, port) tuple, default is (‘’, 6000)
  • loopEventloop instance
  • protocol_cls – thrift protocol class, default is TBinaryProtocol
  • timeout – server side timeout, default is None

a Server object which can be used to stop the service

aiothrift.create_connection(service, address=('', 6000), *, protocol_cls=<class 'aiothrift.protocol.TBinaryProtocol'>, timeout=None, loop=None)[source]

Create a thrift connection. This function is a coroutine.

Open a connection to the thrift server by address argument.

  • service – a thrift service object
  • address – a (host, port) tuple
  • protocol_cls – protocol type, default is TBinaryProtocol
  • timeout – if specified, would raise asyncio.TimeoutError if one rpc call is longer than timeout
  • loopEventloop instance, if not specified, default loop is used.

newly created ThriftConnection instance.

aiothrift.create_pool(service, address=('', 6000), *, minsize=1, maxsize=10, loop=None, timeout=None)[source]

Create a thrift connection pool. This function is a coroutine.

  • service – service object defined by thrift file
  • address – (host, port) tuple, default is (‘’, 6000)
  • minsize – minimal thrift connection, default is 1
  • maxsize – maximal thrift connection, default is 10
  • loop – targeting eventloop
  • timeout – default timeout for each connection, default is None

ThriftPool instance