[Python] Do I need to .acquire() or .transaction() when only fetching data in ASYNCPG?

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    This works:

    db = await asyncpg.create_pool(**credentials, min_size=1, max_size=10)
    pcount = await db.fetchval(query)
    print(f'players: {pcount}')


    But so does this

    db = await asyncpg.create_pool(**credentials, min_size=1, max_size=10)
    async with db.acquire() as con:
    async with con.transaction():
    pcount = await con.fetchval(query)
    print(f'players: {pcount}')
    await db.release(con)


    I am using a pooled connection with asyncpg python and I noticed that I can fetchrow just fine without having to do .acquire() a connection first. Which one is best practice, and should I acquire and transaction for everything or just database update/inserts?

    Continue reading...

Compartilhe esta Página