asyncio与Postgres集成。
项目描述
aiopg 是一个用于从 PostgreSQL 数据库中访问 asyncio(PEP-3156/tulip)框架的库。它封装了 Psycopg 数据库驱动程序的异步功能。
示例
import asyncio
import aiopg
dsn = 'dbname=aiopg user=aiopg password=passwd host=127.0.0.1'
async def go():
pool = await aiopg.create_pool(dsn)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 1")
ret = []
async for row in cur:
ret.append(row)
assert ret == [(1,)]
loop = asyncio.get_event_loop()
loop.run_until_complete(go())
SQLAlchemy 可选集成示例
import asyncio
from aiopg.sa import create_engine
import sqlalchemy as sa
metadata = sa.MetaData()
tbl = sa.Table('tbl', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('val', sa.String(255)))
async def create_table(engine):
async with engine.acquire() as conn:
await conn.execute('DROP TABLE IF EXISTS tbl')
await conn.execute('''CREATE TABLE tbl (
id serial PRIMARY KEY,
val varchar(255))''')
async def go():
async with create_engine(user='aiopg',
database='aiopg',
host='127.0.0.1',
password='passwd') as engine:
async with engine.acquire() as conn:
await conn.execute(tbl.insert().values(val='abc'))
async for row in conn.execute(tbl.select()):
print(row.id, row.val)
loop = asyncio.get_event_loop()
loop.run_until_complete(go())
请使用
$ make test
来执行项目的单元测试。有关如何设置测试环境的详细信息,请参阅 https://aiopg.readthedocs.io/en/stable/contributing.html。
变更日志
1.4.0 (2022-10-26)
添加 python 3.11 并停止对 python 3.6 的支持` #892 <https://github.com/aio-libs/aiopg/pull/892>`_
1.3.5 (2022-09-25)
修复池大小限制检查以支持无限制池 #888
1.3.4 (2022-06-30)
1.3.4b3 (2022-06-29)
1.3.4b2 (2022-06-29)
1.3.4b1 (2022-06-29)
1.3.3 (2021-11-01)
支持 async-timeout 4.0+
1.3.2 (2021-10-07)
1.3.2b2 (2021-10-07)
尊重 select 语句的 use_labels #882
1.3.2b1 (2021-07-11)
修复与 SQLAlchemy >= 1.4 的兼容性 #870
1.3.1 (2021-07-08)
1.3.1b2 (2021-07-06)
抑制“Future 异常未被检索” #862
1.3.1b1 (2021-07-05)
修复 ClosableQueue.get 在取消时的行为,在 Connection.close 时关闭它 #859
1.3.0 (2021-06-30)
1.3.0b4 (2021-06-28)
修复“无法检测使用 NOTIFY/LISTEN 时的断开连接” #559
1.3.0b3 (2021-04-03)
使用 black 重新格式化 #814
1.3.0b2 (2021-04-02)
类型注解 #813
1.3.0b1 (2021-03-30)
如果尝试在关闭的 SAConnection 上打开游标,则引发 ResourceClosedError #811
1.3.0b0 (2021-03-25)
修复与 SA 1.4 的兼容性,针对 IN 语句 #806
1.2.1 (2021-03-23)
由于向后兼容性,在连接初始化时弹出循环 #808
1.2.0b4 (2021-03-23)
设置最大支持的 sqlalchemy 版本 #805
1.2.0b3 (2021-03-22)
1.2.0b2 (2020-12-21)
1.2.0b1 (2020-12-16)
弃用 blocking connection.cancel() 方法 #570
1.2.0b0 (2020-12-15)
在从池获取连接时实现超时 #766
1.1.0 (2020-12-10)
1.1.0b2 (2020-12-09)
添加了上下文管理器中缺少的槽位 #763
1.1.0b1 (2020-12-07)
1.0.0 (2019-09-20)
移除异步调用,以支持 issues # 550
文档大编辑和轻微错误 #534
0.16.0 (2019-01-25)
0.15.0 (2018-08-14)
支持 Python 3.7 #437
0.14.0 (2018-05-10)
添加 get_dialect 函数以传递 json_serializer #451
0.13.2 (2018-01-03)
0.13.1 (2017-09-10)
添加连接轮询回收逻辑 #373
0.13.0 (2016-12-02)
0.12.0 (2016-10-09)
0.11.0 (2016-09-12)
立即从关闭的文件描述符中删除回调 #139
删除 Python 3.3 支持
0.10.0 (2016-07-16)
0.9.2 (2016-01-31)
0.9.1 (2016-01-17)
更新文档 #101
0.9.0 (2016-01-14)
0.8.0 (2015-12-31)
0.7.0 (2015-04-22)
解决连接失败时的资源泄漏问题。
在非关闭连接上报告 ResourceWarning。
弃用 cursor 和 ResultProxy 中的迭代协议支持。
在 connection.close() 时释放 sa 连接到池。
0.6.0 (2015-02-03)
在 SAConnection.execute() 中接受 dict、list、tuple、命名和位置参数
0.5.2 (2014-12-08)
小版本发布,修复了在 cursor.execute() 失败后留下连接处于损坏状态的问题。
0.5.1 (2014-10-31)
修复了处理行事务的 bug。
0.5.0 (2014-10-31)
向 Pool 和 Engine 添加 .terminate()
重新实现连接池(现在池大小不能超过 pool.maxsize)
向 Pool 和 Engine 添加 .close() 和 .wait_closed() 方法
向 sa.Engine 添加 minsize、maxsize、size 和 freesize 属性
支持 echo 参数以记录执行的 SQL 命令
Connection.close() 不是一个协程(但我们将保留向后兼容性)。
0.4.1 (2014-10-02)
使游标可迭代
更新文档
0.4.0 (2014-10-02)
为数据库操作添加超时
自动注册 psycopg2 对 json 数据类型的支持
在 aiopg.sa 中支持 JSON
在 aiopg.sa 中支持 ARRAY
如果连接的数据库中存在,自动注册 hstore 支持
在 aiopg.sa 中支持 HSTORE
0.3.2 (2014-07-07)
将签名更改为 cursor.execute(operation, parameters=None),以遵循 psycopg2 规范。
0.3.1 (2014-07-04)
将参数传递给池连接的游标构造函数
0.3.0 (2014-06-22)
允许执行 SQLAlchemy DDL 语句。
修复在获取/释放连接时的竞态条件问题。
0.2.3 (2014-06-12)
修复连接池中的错误。
0.2.2 (2014-06-07)
修复在执行原始 SQL 表达式时传递参数到 SAConnection.execute 的错误。
0.2.1 (2014-05-08)
在返回池时关闭具有无效事务状态的连接。
0.2.0 (2014-05-04)
实现了对 sqlalchemy 函数 SQL 层的可选支持。
0.1.0 (2014-04-06)
实现了普通连接:connect、Connection、Cursor。
实现了数据库池:create_pool 和 Pool。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。