跳转到主要内容

异步IO的MySQL驱动程序。

项目描述

https://github.com/aio-libs/aiomysql/actions/workflows/ci-cd.yml/badge.svg?branch=master Code coverage Latest Version Documentation Status Chat on Gitter

aiomysql 是一个从 asyncio(PEP-3156/tulip)框架中访问 MySQL 数据库的“驱动程序”。它依赖于并重用 PyMySQL 的大部分功能。 aiomysql 尝试与出色的 aiopg 库相似,并保留相同的 API、外观和感觉。

内部上,aiomysql 是 PyMySQL 的副本,底层的 io 调用已切换为异步,基本上在合适的位置添加了 yield fromasyncio.coroutine)。 sqlalchemy 支持(从 aiopg)移植过来。

文档

https://aiomysql.readthedocs.io/

基本示例

aiomysql 基于 PyMySQL,并提供相同的 API,您只需使用 await conn.f()yield from conn.f() 而不是为每个方法调用 conn.f()

属性未更改,因此 conn.prop 是正确的,同样 conn.prop = val 也是正确的。

import asyncio
import aiomysql


async def test_example(loop):
    pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
                                      user='root', password='',
                                      db='mysql', loop=loop)
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT 42;")
            print(cur.description)
            (r,) = await cur.fetchone()
            assert r == 42
    pool.close()
    await pool.wait_closed()


loop = asyncio.get_event_loop()
loop.run_until_complete(test_example(loop))

SQLAlchemy 可选集成的示例

SQLAlchemy 支持已从 aiopg 移植过来,因此 API 应该对 aiopg 用户非常熟悉。

import asyncio
import sqlalchemy as sa

from aiomysql.sa import create_engine


metadata = sa.MetaData()

tbl = sa.Table('tbl', metadata,
               sa.Column('id', sa.Integer, primary_key=True),
               sa.Column('val', sa.String(255)))


async def go(loop):
    engine = await create_engine(user='root', db='test_pymysql',
                                 host='127.0.0.1', password='', loop=loop)
    async with engine.acquire() as conn:
        await conn.execute(tbl.insert().values(val='abc'))
        await conn.execute(tbl.insert().values(val='xyz'))

        async for row in conn.execute(tbl.select()):
            print(row.id, row.val)

    engine.close()
    await engine.wait_closed()


loop = asyncio.get_event_loop()
loop.run_until_complete(go(loop))

需求

更改

0.2.0 (2023-06-11)

  • 将最小 SQLAlchemy 版本提升到 1.3 #815

  • 删除已弃用的 Pool.get #706

  • 部分移植 PyMySQL#304 #792
    aiomysql 现在在连接()期间重新抛出原始异常,如果不是 IOErrorOSErrorasyncio.TimeoutError
    这以前总是被作为 OperationalError 抛出。
  • 修复使用 sha256_password 身份验证时的调试日志级别 #863

  • 使用 pyupgrade 将代码现代化为 Python 3.7+ 语法 #930

  • 移除对已停止服务的 MariaDB 版本 10.3、10.7 和 10.8 的测试,添加对 MariaDB 10.9、10.10、10.11 的测试 #932

0.1.1 (2022-05-08)

  • 修复 SSL 连接握手字符集不尊重客户端配置 #776

0.1.0 (2022-04-11)

  • 默认情况下,不要将 sys.argv[0] 作为 program_name 发送到 MySQL 服务器 #620

  • 允许以匿名 uid 运行进程 #587

  • 修复超时 MySQL 8.0 连接抛出 InternalError 而不是 OperationalError #660

  • 修复超时 MySQL 8.0 连接从 Pool 返回 #660

  • 在服务器连接丢失时,在抛出 OperationalError 之前确保连接被正确关闭 #660

  • 在数据包序列号不一致时,在抛出 InternalError 之前确保连接被正确关闭 #660

  • Unix 套接字现在内部被认为是安全的,允许使用 sha256_password 和 caching_sha2_password 身份验证方法 #695

  • 测试套件现在也测试 Unix 套接字连接 #696

  • 修复 SSCursor 在最后结果未完全检索时抛出 InternalError #635

  • 删除已弃用的 no_delay 参数 #702

  • 支持 PyMySQL 至版本 1.0.2 #643

  • 将最小 PyMySQL 版本提升到 1.0.0 #713

  • 使 Cursor.executemany() 中的 % 格式化与 Cursor.execute() 保持一致,现在在 Cursor.executemany() 中需要将字面量 % 重复两次 #714

  • 固定无限池大小现在不起作用,现在通过传递 maxsize=0 到 create_pool 来实现,如文档所述 #119

  • 添加了 Pool.closed 属性,与 aiopg 中的属性相同 #463

  • 修复了 SQLAlchemy 连接上下文迭代器 #410

  • 修复了 SSCursor 的错误包处理 #428

  • 所需的 python 版本现在已在 python_requires 中正确记录,而不是在 setup.py 执行时失败 #731

  • 根据 PyMySQL[rsa] 添加 rsa extras_require #557

  • 迁移到 PEP 517 构建系统 #746

  • 自动报告的 __version__ 现在返回构建期间由 setuptools-scm 生成的版本,否则返回 ‘unknown’ #748

  • 修复了 SSCursor 在错误查询上引发查询超时错误的问题 #428

0.0.22 (2021-11-14)

  • 支持 python 3.10 #505

0.0.21 (2020-11-26)

  • 允许使用自定义 Cursor 子类 #374

  • 为 Connection 类填充实际客户端版本 #388

  • 修复了 legacy __aiter__ 方法 #403

  • 修复并更新了文档 #418 #437

  • 忽略 pyenv 的 .python-version 文件 #424

  • 将 asyncio.streams.IncompleteReadError 替换为 asyncio.IncompleteReadError #460 #454

  • 添加了对 SQLAlchemy 默认参数的支持 #455 #466

  • 更新依赖项 #485

  • 支持 Python 3.7 和 3.8 #493

0.0.20 (2018-12-19)

  • 修复了 connect_timeout #360

  • 修复了对 SQLA executemany 的支持 #324

  • 修复了 python 3.7 兼容性问题 #357

  • 修复了当 StreamReader 有异常时重用连接的问题 #339

  • 修复了插入二进制字符串时的警告 #326

0.0.19 (2018-07-12)

  • 查看 v0.0.18

0.0.18 (2018-07-09)

  • 更新以支持最新的 PyMySQL 变更。

  • aiomysql 现在发送客户端连接信息。

  • 支持 MySQL8+,包括 sha256_password 和 cached_sha2_password 身份验证插件。

  • 发送到服务器的默认最大数据包长度不再是 1。

  • 修复了 cursor.nextset 在引发错误的查询集中挂起的问题。

0.0.17 (2018-07-06)

  • 固定 PyMySQL 版本

0.0.16 (2018-06-03)

  • 添加了执行预编译 sqlalchemy 查询的能力 #294(感谢 @vlanse)

0.0.15 (2018-05-20)

  • 修复了 sqlalchemy 中的用户定义类型的处理 #290

  • 修复了服务器报告未知校准时出现的 KeyError #289

0.0.14 (2018-04-22)

  • 修复了 SSL 连接最终化问题 #282

0.0.13 (2018-04-19)

  • 添加了 SSL 支持 #280(感谢 @terrycain)

  • 修复了 aiomysql/__init__ 中的 __all__ #270(感谢 @matianjun1)

  • 添加了 docker 固件 #275(感谢 @terrycain)

0.0.12 (2018-01-18)

  • 修复了对 SQLAlchemy 1.2.0 的支持

  • 修复了 sa engine 中 cursor.execute 的参数 #239(感谢 @NotSoSuper)

0.0.11 (2017-12-06)

  • 修复了 pypi 上的 README 格式化问题

0.0.10 (2017-12-06)

  • 更新正则表达式以与 pymysql 兼容 #167(感谢 @AlexLisovoy)

  • 在池中添加了连接回收功能 #216

0.0.9 (2016-09-14)

  • 修复了 _request_authentication 函数中的 AttributeError #104(感谢 @ttlttl)

  • 修复了旧版身份验证 #105

  • 将 uvloop 添加到测试套件中 #106

  • 修复了 json 字段中的 unicode 错误 #107(感谢 @methane)

0.0.8 (2016-08-24)

  • 将默认最小池大小减少到 1 #80(感谢 @Drizzt1991)

  • 更新到 PyMySQL 0.7.5 #89

  • 修复了在执行查询过程中连接取消的问题 #79(感谢 @Drizzt1991)

0.0.7 (2016-01-27)

  • 修复了多结果问题,从 pymysql 转移过来 #52

  • 修复了无延迟选项的无效警告 #55

  • 为 Engine、SAConnection、Transaction 添加了 async/await 支持 #57

  • pool.release 返回 future,因此我们可以在 __aexit__ 中等待它 #60

  • 更新到 PyMySQL 0.6.7

0.0.6 (2015-12-11)

  • 修复了 SA 回滚错误(感谢 @khlyestovillarion!)

  • 修复了默认无延迟选项的问题(感谢 @khlyestovillarion!)

0.0.5 (2015-10-28)

  • no_delay 选项已弃用,默认为 True

  • 添加 Cursor.mogrify() 方法

  • 支持“LOAD LOCAL INFILE”查询。

  • 在池内检查连接,如果超时则丢弃它,修复了 #25

  • 将 pool、connection 和 cursor 中的 python 3.5 功能添加到支持中

0.0.4 (2015-05-23)

  • 允许调用 connection.wait_closed 两次。

  • 修复了对 sqlalchemy 1.0.0 的支持。

  • 修复 #11:将 Connection.wait_closed() 重命名为 .ensure_closed()

  • 在非关闭的 Connection 上引发 ResourceWarning

  • 将 Connection.connect 重命名为 _connect

0.0.3 (2015-03-10)

  • 添加了对 PyMySQL 0.6.6 以上的支持。

  • 移植自 PyMySQL 的改进。

  • 添加了基本文档。

  • 修复并添加了更多示例。

0.0.2 (2015-02-17)

  • 添加了 MANIFEST.in。

0.0.1 (2015-02-17)

  • 初始发布。

  • 实现了普通连接:connect、Connection、Cursor。

  • 实现了数据库连接池。

  • 移植了 sqlalchemy 可选支持。

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分布

aiomysql-0.2.0.tar.gz (114.7 kB 查看散列值)

上传时间

构建分布

aiomysql-0.2.0-py3-none-any.whl (44.2 kB 查看散列值)

上传时间 Python 3

由以下支持:

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页