A Thrift client connection pool.
a convenient way to integrate the pool with your application.
The pool lazily creates connections and maintains them in a pool. Individual connections have a maximum lifetime, after which they will be recycled.
A basic example of usage:
pool = thrift_pool_from_config(app_config, "example_service.") with pool.connection() as protocol: client = ExampleService.Client(protocol) client.do_example_thing()
thrift_pool_from_config(app_config, prefix, **kwargs)¶
Make a ThriftConnectionPool from a configuration dictionary.
The keys useful to
thrift_pool_from_config()should be prefixed, e.g.
prefixargument specifies the prefix used to filter keys. Each key is mapped to a corresponding keyword argument on the
ThriftConnectionPoolconstructor. Any keyword arguments given to this function will be also be passed through to the constructor. Keyword arguments take precedence over the configuration file.
where the Thrift server can be found.
size: The size of the connection pool.
max_age: The oldest a connection can be before it’s recycled and
replaced with a new one. Written as a
timeout: The maximum amount of time a connection attempt or RPC call
can take before a TimeoutError is raised. (
max_connection_attempts: The maximum number of times the pool will attempt to
open a connection.
Changed in version 1.2:
- Return type
ThriftConnectionPool(endpoint, size=10, max_age=120, timeout=1, max_connection_attempts=3, protocol_factory=<thrift.protocol.THeaderProtocol.THeaderProtocolFactory object>)¶
A pool that maintains a queue of open Thrift connections.
EndpointConfiguration) – The remote address of the Thrift service.
int) – The maximum number of connections that can be open before new attempts to open block.
int) – The maximum number of seconds a connection should be kept alive. Connections older than this will be reaped.
float) – The maximum number of seconds a connection attempt or RPC call can take before a TimeoutError is raised.
int) – The maximum number of times the pool will attempt to open a connection.
TProtocolFactory) – The factory to use for creating protocols from transports. This is useful for talking to services that don’t support THeaderProtocol.
All exceptions raised by this class derive from
Changed in version 1.2:
Acquire a connection from the pool.
This method is to be used with a context manager. It returns a connection from the pool, or blocks up to
timeoutseconds waiting for one if the pool is full and all connections are in use.
When the context is exited, the connection is returned to the pool. However, if it was exited via an unexpected Thrift exception, the connection is closed instead because the state of the connection is unknown.