Skip to main content

Async database connection pool utilities for PostgreSQL (Redis, MySQL planned)

Project description

async-db-tools

基于 asyncpg 的 PostgreSQL 异步连接池封装,提供带自动重试的常用查询 API。后续计划支持 Redis、MySQL 等。

安装

pip install async-db-tools
# 或
uv add async-db-tools

快速开始

import asyncio

from async_db_tools import PostgresPool


async def main() -> None:
    async with PostgresPool("postgresql://user:pass@localhost:5432/mydb") as db:
        row = await db.fetchrow("SELECT 1 AS n")
        print(row["n"])


asyncio.run(main())

也可手动管理生命周期:

db = PostgresPool(
    "postgresql://user:pass@localhost:5432/mydb",
    min_size=2,
    max_size=20,
    max_retries=3,
)
await db.connect()
try:
    await db.execute("INSERT INTO t (v) VALUES ($1)", 42)
finally:
    await db.close()

API

方法 说明
connect() 创建连接池(幂等)
close() 关闭连接池
execute(query, *args) 执行写操作
fetch / fetchrow / fetchval 查询
pool 底层 asyncpg.Pool,用于事务等高级用法

连接类错误(网络断开等)会自动重试,次数由 max_retries 控制。

项目结构

src/async_db_tools/
├── __init__.py
└── postgresql/          # PostgreSQL
    ├── __init__.py
    └── pool.py
# 后续: redis/, mysql/ ...

开发

uv sync
uv build

许可证

MIT(发布前请自行添加 LICENSE 文件并填写作者信息。)

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

async_db_tools-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

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

async_db_tools-0.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file async_db_tools-0.1.0.tar.gz.

File metadata

  • Download URL: async_db_tools-0.1.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for async_db_tools-0.1.0.tar.gz
Algorithm Hash digest
SHA256 df7871e902aca3ed2057dad1b6d2a8660151703bcd4c0fab06437b1e45d9daca
MD5 c8125687cd9fd129a6a52f2ffcf6f1ac
BLAKE2b-256 b2cdfb1d6dbe6a12ac807f1d8e1abe9f60e6f80c2447fe7607f0a91129d38a90

See more details on using hashes here.

File details

Details for the file async_db_tools-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: async_db_tools-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for async_db_tools-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 328e2c7337d2282e6103c3fe81a03fbe02e968a7dc26cbacde514fa982967dd2
MD5 3559f1f170343d1d3e1855eb4ec68a64
BLAKE2b-256 587fe51ad830cf322b1bf21a62643970b514f69e89bb19c3f8e00427e2bf281d

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