Loading

10.异步mysql

python中操作mysql连接、操作、断开都是网络IO

#安装支持异步aiomysql的模块
pip3 install aiomysql
async def execute():
    # 网络IO操作,连接数据库,遇到IO切换任务
    conn = await aiomysql.connect('host', 3306, 'root', 'password', 'db')
    # 网络IO操作,遇到IO自动切换任务
    cur = await conn.cursor()
    # 网络IO操作,遇到IO自动切换任务
    await cur.execute('select f from table')
    res = await cur.fetchall()
    print(res)
    # 网络IO操作,遇到IO自动切换任务
    await cur.close()
    conn.close()

 

posted @ 2022-08-21 23:18  木子七  阅读(53)  评论(0编辑  收藏  举报