import asyncio
import aiomysql
async def create_pool():
pool = await aiomysql.create_pool(
host='localhost',
port=3306,
user='root',
password='password',
db='mydb',
minsize=5,
maxsize=10
)
return pool
async def execute_query(pool):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT * FROM mytable")
result = await cur.fetchall()
print(result)
async def close_pool(pool):
pool.close()
await pool.wait_closed()
async def main():
pool = await create_pool()
await execute_query(pool)
await close_pool(pool)
asyncio.run(main())