Python异步操作MySQL示例【使用aiomysql】
本文实例讲述了Python异步操作MySQL。分享给大家供大家参考,具体如下:
安装aiomysql
依赖
Python3.4+
asyncio
PyMySQL
安装
pip install aiomysql
应用
基本的异步连接connection
import asyncio
from aiomysql import create_pool
loop = asyncio.get_event_loop()
async def go():
async with create_pool(host='127.0.0.1', port=3306,
user='root', password='',
db='mysql', loop=loop) as pool:
async with pool.get() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 42;")
value = await cur.fetchone()
print(value)
loop.run_until_complete(go())
异步的连接池 pool
import asyncio
import aiomysql
async def test_example(loop):
pool = await aio