asyncpg - connection vs connection pool

前端 未结 2 1664
广开言路
广开言路 2021-02-04 15:18

I am going over asyncpg\'s documentation, and I am having trouble understanding why use a connection pool instead of a single connection.

In the example given, a pool is

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-04 15:43

    Elvis Pranskevichus showed two benchmarks above. I took the first one bench_asyncpg_con and edited it, moving the con outside the loop:

    async def bench_asyncpg_con_2():
        power = 2
        start = time.monotonic()
        con = await asyncpg.connect(**data)
        for i in range(1, 1000):
            await con.fetchval('select 2 ^ $1', power)
    
        await con.close()
        end = time.monotonic()
        print(end - start) 
    

    And this worked much faster than bench_asyncpg_pool

    I got 0.45 on this, whereas I got

    1.62 on bench_asyncpg_pool and

    63.18 on bench_asyncpg_con

    I'm kinda newbie in using this lib and thinking that one connection con for a whole project would be a good choice. Correct me if I'm wrong. I will appreciate it

提交回复
热议问题