Skip to main content

An asyncio-compatible socket pool

Project description

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

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):
    writer.write(await reader.read(32))
    await writer.drain()
    writer.close()


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)

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")
        print(await conn.recv(32))


await hello_world()

Create a bunch of connections and run them all concurrently.

loop = asyncio.get_event_loop()

tasks = []

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

loop.run_until_complete(asyncio.gather(*tasks))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiosocketpool-0.0.5.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiosocketpool-0.0.5-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file aiosocketpool-0.0.5.tar.gz.

File metadata

  • Download URL: aiosocketpool-0.0.5.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for aiosocketpool-0.0.5.tar.gz
Algorithm Hash digest
SHA256 0d5090b69f22fa30dfcd57bf3a80b985b8177dd4ea29215e3d248476fc5d0146
MD5 cb8fd790c8fe2896288401947113186c
BLAKE2b-256 174fbdf4e7ec6a294f1c2bd868d300292b450688ddcf1180ac7ac65a88ba0cba

See more details on using hashes here.

File details

Details for the file aiosocketpool-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: aiosocketpool-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for aiosocketpool-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9285926ab45c787005467fa74075e7780129b10146cbdf6ae86183f49302791b
MD5 34fa6d6867ff78c2de52f4b31ce7fb48
BLAKE2b-256 50bd72504beec57cda14190a475305f6ca33f730da06b90baafb2d87b7bab9a0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page