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 --extra dev
uv build
测试
cp .env.example .env # 填写 PG_DSN
uv sync --extra dev
uv run pytest -v
未设置 PG_DSN 时,依赖数据库的用例会自动跳过。
许可证
Project details
Release history Release notifications | RSS feed
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.1.tar.gz
(5.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file async_db_tools-0.1.1.tar.gz.
File metadata
- Download URL: async_db_tools-0.1.1.tar.gz
- Upload date:
- Size: 5.0 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6436c1bdbcd0993f1ea6dae0acd088925f2edc25ea39a7952f35f12e6a30e2fa
|
|
| MD5 |
40cf4b2f549acaa7dd52c1a6621dee4b
|
|
| BLAKE2b-256 |
e5235386c12737b375d22d554ad69a94ff518c972659920f7591b0bbd9e68074
|
File details
Details for the file async_db_tools-0.1.1-py3-none-any.whl.
File metadata
- Download URL: async_db_tools-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.8 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37560efea38f71edbc69ac95115ed6c214016f234ec81fa76364a390a6faa0de
|
|
| MD5 |
ba518836eb1625f35d2b90a9d7760186
|
|
| BLAKE2b-256 |
b758977cb2a1dd5b4ebe5d9f1093dc523cc4c02a93bce5e4d538d341aea1d795
|