API

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.

_init_rpc_apis()[source]

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.

_recv(api)[source]

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.

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

result of this rpc call

Raises:

TimeoutError if this task has exceeded the timeout

Raises:

ThriftAppError if thrift response is an exception defined in thrift.

Raises:

ConnectionClosedError: if server has closed this connection.

ThriftConnection Pool

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

Thrift connection pool.

acquire()[source]

Acquires a connection from free pool.

Creates new connection if needed.

clear()[source]

Clear pool connections.

Close and remove all free connections. this pattern is interesting

close()[source]

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.

freesize

Current number of free connections.

release(conn)[source]

Returns used connection back into pool.

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

size

Current connection total num, acquiring connection num is counted

protocol

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.

processor

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

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

server

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

exceptions

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=('127.0.0.1', 6000), loop=None, protocol_cls=<class 'aiothrift.protocol.TBinaryProtocol'>, timeout=None)[source]

create a thrift server. This function is a coroutine.

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

a Server object which can be used to stop the service

aiothrift.create_connection(service, address=('127.0.0.1', 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.

Parameters:
  • 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.
Returns:

newly created ThriftConnection instance.

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

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

Parameters:
  • service – service object defined by thrift file
  • address – (host, port) tuple, default is (‘127.0.0.1’, 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
Returns:

ThriftPool instance