SourceRank

Documentation

txconnpool

A generalized connection pooling library for Twisted.

Example Description

Assume that we've got a web application, which performs some expensive
computations, and then caches them in a memcached server. The simple way to
achieve this in Twisted is to create a ClientCreator for the MemCacheProtocol
and whenever we need to communicate with the server, we can simply use that.

This works for low volumes of queries, but let's say that now we start hitting
memcached a lot--several times per web request, of which we are receiving many
per second. Very quickly, the connection overhead can become a problem.

Instead of creating a new connection for every query, it would be much better
to maintain a pool of open connections, and simply reuse those open
connections; queuing up any queries if all of the connections are in use. With
txconnpool, setting this up can be quite easy.

Example Implementation

First we need to create a few classes of boilerplate, to transform a
MemCacheProtocol into a PooledMemcachedProtocol, and then create a pool: