Skip to main content

asyncio-connection-pool

This is a generic, high-throughput, optionally-burstable pool for asyncio.

Some cool features:

  • No locking aside from the GIL; no asyncio.Lock or asyncio.Condition needs to be taken in order to get a connection.
  • Available connections are retrieved without yielding to the event loop.
  • When burst_limit is specified, max_size acts as a "soft" limit; the pool can go beyond this limit to handle increased load, and shrinks back down after.
  • The contents of the pool can be anything; just implement a ConnectionStrategy.

Why?

We were using a different pool for handling our Redis connections, and noticed that, under heavy load, we would spend a lot of time waiting for the lock, even when there were available connections in the pool.

We also thought it would be nice if we didn't need to keep many connections open when they weren't needed, but still have the ability to more when they are required.

API

asyncio_connection_pool.ConnectionPool

This is the implementation of the pool. It is generic over a type of connection, and all implementation-specific logic is contained within a ConnectionStrategy.

A pool is created as follows:

from asyncio_connection_pool import ConnectionPool

pool = ConnectionPool(strategy=my_strategy, max_size=15)

The constructor can optionally be passed an integer as burst_limit. This allows the pool to open more connections than max_size temporarily.

@asynccontextmanager async def get_connection(self) -> AsyncIterator[Conn]

This method is the only way to get a connection from the pool. It is expected to be used as follows:

pool = ConnectionPool(...)

async with pool.get_connection() as conn:
    # Use the connection
    pass

When the async with block is entered, a connection is retrieved. If a connection needs to be opened or if the pool is at capacity and no connections are available, the caller will yield to the event loop.

When the block is exited, the connection will be returned to the pool.

asyncio_connection_pool.ConnectionStrategy

This is an abstract class that defines the interface of the object passed as strategy. A subclass must implement the following methods:

async def create_connection(self) -> Awaitable[Conn]

This method is called to create a new connection to the resource. This happens when a connection is requested and all connections are in use, as long as the pool is not at capacity.

The result of a call to this method is what will be provided to a consumer of the pool, and in most cases will be stored in the pool to be re-used later.

If this method raises an exception, it will bubble up to the frame where ConnectionPool.get_connection() was called.

def connection_is_closed(self, conn: Conn) -> bool

This method is called to check if a connection is no longer able to be used. When the pool is retrieving a connection to give to a client, this method is called to make sure it is valid.

The return value should be True if the connection is not valid.

If this method raises an exception, it is assumed that the connection is invalid. The passed-in connection is dropped and a new one is retrieved. The exception is suppressed unless it is not a BaseException, like asyncio.CancelledError. It is the responsibility of the ConnectionStrategy implementation to avoid leaking a connection in this case.

def close_connection(self, conn: Conn)

This method is called to close a connection. This occurs when the pool has exceeded max_size (i.e. it is bursting) and a connection is returned that is no longer needed (i.e. there are no more consumers waiting for a connection).

Note that this method is synchronous; if closing a connection is an asynchronous operation, asyncio.create_task can be used.

If this method raises an exception, the connection is dropped and the exception bubbles to the caller of ConnectionPool.get_connection().__aexit__ (usually an async with block).

How is this safe without locks?

I encourage you to read the source to find out (it is quite well-commented). If you notice any faults in the logic, please feel free to file an issue.

Metadata

Release files for asyncio-connection-pool 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncio-connection-pool 0.1.0
File Size Uploaded Provenance Actions
asyncio-connection-pool-0.1.0.tar.gz 11.9 kB Without provenance Details for asyncio-connection-pool-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for asyncio-connection-pool 0.1.0
File Interpreter ABI Platform Actions
asyncio_connection_pool-0.1.0-py3-none-any.whl Python 3 none any Details for asyncio_connection_pool-0.1.0-py3-none-any.whl

Total release size: 16.1 kB

Release files / asyncio-connection-pool-0.1.0.tar.gz

Download URL asyncio-connection-pool-0.1.0.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
39b851e17fe6534003b433d5fe7fc85d69239f62c5005ccb129e2e5bb54431fc
BLAKE2b-256 checksum
How to use checksums
7768ae88fd73c1fdacf44a7f9519e780ca4f85627fdc1d4da1dd7c0719c4cdc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / asyncio_connection_pool-0.1.0-py3-none-any.whl

Download URL asyncio_connection_pool-0.1.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d01b7e49fcd57ab777dedcfb72159702f96a890e452ea0f471a80768110418e0
BLAKE2b-256 checksum
How to use checksums
5ca9e42c1a8e60f978eb855f787314b1d2a793516e4d14476284a13782649f05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Security for asyncio-connection-pool 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page