Skip to main content

Code style: black CI

aiosocketpool

An asyncio-compatible socket pool. Simple, compact, easily extended.

If your application needs to connect to many remote hosts simultaneously (and often), it probably makes sense to keep connections open and re-use them rather than establishing a new connection for every request. Combining an asyncio event loop and a socket pool might be the way to go!

Based on socketpool.

Requires Python 3.8 or above.

Examples

You can run these examples, one after another, in a python -m asyncio shell.

  1. Run a simple TCP echo server in a background thread, using the asyncio library.
import asyncio
import threading


# start a new event loop running in a background thread
def run_loop_forever(loop):
    loop.run_forever()


loop = asyncio.new_event_loop()

t = threading.Thread(name="BackgroundEventLoop", target=run_loop_forever, args=[loop], daemon=True)
t.start()


# run a tcp echo server using asyncio in the background event loop
async def echo_handler(reader, writer):
    try:
        while True:
            try:
                data = await reader.readuntil(b'\n')
            except asyncio.IncompleteReadError:
                break

            writer.write(data)
            await writer.drain()

    finally:
        writer.close()
        await writer.wait_closed()


async def echo_server(tcp_port):
    server = await asyncio.start_server(echo_handler, "127.0.0.1", tcp_port)
    await server.serve_forever()


asyncio.run_coroutine_threadsafe(echo_server(12345), loop)
  1. Create a new TCP connection pool in the main thread, get a connection, and send and receive data.
from aiosocketpool import AsyncConnectionPool, AsyncTcpConnector


pool = AsyncConnectionPool(
    factory=AsyncTcpConnector,
    reap_connections=True,  # a background task will destroy old and idle connections
    max_lifetime=10,  # connections will remain idle at most 10 seconds
    max_size=10,  # we will maintain at most 10 idle connections in the pool
)


async def hello_world():
    async with pool.connection(host="127.0.0.1", port=12345) as conn:
        await conn.sendall(b"hello world\n")
        print(await conn.recv(32))


await hello_world()
  1. Create a bunch of connections and run them all concurrently.
tasks = []

for _ in range(25):
    tasks.append(asyncio.create_task(hello_world()))

await asyncio.gather(*tasks)

Release files for aiosocketpool 0.0.6

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

Source distribution (sdist)

Source distribution for aiosocketpool 0.0.6
File Size Uploaded
aiosocketpool-0.0.6.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiosocketpool 0.0.6
File Interpreter ABI Platform
aiosocketpool-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 18.5 kB

Release files / aiosocketpool-0.0.6.tar.gz

Download URL aiosocketpool-0.0.6.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b9e923971fdcd3042200291fa6e94714b6cc4ddba86662ab85be8e31b18de99e
BLAKE2b-256 checksum
How to use checksums
550bd0724d3cd5c88346084c8f9b33971518f8eab1e3f74bd02c8641a03f2f29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / aiosocketpool-0.0.6-py3-none-any.whl

Download URL aiosocketpool-0.0.6-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e2e977d566d90d429c86bf19a47e5e7a4530679fd31ce59397987e66267d80f
BLAKE2b-256 checksum
How to use checksums
39dc3fa4a8ae6bdb0abd76a5d415a590efe709633df97b9abedfac618dcbd762
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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