使用池连接——aiomysql
我的代码如下所示:
# POOL CONNECTION
# create pool connection
async def create_pool():
print("Creating pool connection...")
global pool
pool = await aiomysql.create_pool(
host=DB_HOST,
port=DB_PORT,
user=DB_USER,
password=DB_PASSWORD,
db=DB_DBNAME,
autocommit=True
)
async def get_connection():
async with pool.acquire() as conn:
return conn
pool.close()
await pool.wait_closed()
connection = await get_connection()
async with connection.cursor() as cursor:
await cursor.execute(...)
如果发出单个请求,它会连接到 mysql,它会正常运行,但如果同时发出 2 个或更多请求,则会崩溃并抛出此错误:
当另一个协程已经在等待传入数据时调用 readexactly()