【问题标题】:fastapi + aiomysql connection pool stuck after 10 callsfastapi + aiomysql 连接池在 10 次调用后卡住
【发布时间】:2021-02-16 10:30:16
【问题描述】:

为什么aiomysql连接池在N次调用后卡住了? (N是maxsize的连接数。尝试了默认的N=10和N=3)

我认为获得的连接会在退出时自动关闭async with

这是重现的最小脚本:

from fastapi import FastAPI
import aiomysql
import secret

app = FastAPI()

@app.on_event("startup")
async def _startup():
    app.state.pool = await aiomysql.create_pool(host=secret.DB_URL, port=3306, user=secret.DB_USERNAME, password=secret.DB_PASSWORD, db=secret.DB_DATABASE)
    print("startup done")

async def _get_query_with_pool(pool):
    async with await pool.acquire() as conn:
        async with conn.cursor(aiomysql.DictCursor) as cur:
            await cur.execute("SELECT 1")
            return await cur.fetchall()

@app.get("/v1/get_data")
async def _get_data():
    return await _get_query_with_pool(app.state.pool)


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)

【问题讨论】:

    标签: python python-asyncio fastapi aiopg aio-mysql


    【解决方案1】:

    原来罪魁祸首是 pool.acquire() 之前的额外等待

    async def _get_query_with_pool(pool):
        async with await pool.acquire() as conn:
            async with conn.cursor(aiomysql.DictCursor) as cur:
                await cur.execute("SELECT 1")
                return await cur.fetchall()
    

    去掉pool.acquire()之前的额外等待,所以:

    async def _get_query_with_pool(pool):
        async with pool.acquire() as conn:
            async with conn.cursor(aiomysql.DictCursor) as cur:
                await cur.execute("SELECT 1")
                return await cur.fetchall()
    

    现在连接成功

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 2021-09-25
      相关资源
      最近更新 更多