python-3.xpostgresqlpsycopg2asyncpg

The difference between asyncpg and psycopg2


Why does the asyncpg library exist in python if PostgreSQL already supports many simultaneous connections at once? Can anyone explain the difference, and in which cases which library should be used?

It's just important for me to find out the difference, since I plan to create discord bots where there are a lot of async functions, and if psycopg2 blocks the script like time.sleep() does in the async function, then it won't suit me.


Solution

  • Why does the asyncpg library exist in python if

    Libraries exist because someone felt like writing them. There is no one in charge of telling people they aren't allowed to write new libraries if they want to. So the existence is not evidence of the necessity. But...

    PostgreSQL already supports many simultaneous connections at once?

    But, how do you make use of that? The main methods of psycopg2 are blocking. When you dispatch a query, it blocks until the results are ready and fetched. You can have another connection open, but you can't easily get control back in order to do something with that other connection. There are ways around this limitation but they were bolted on as an after thought, while asyncpg provides nicer ways more consistent with how other async programming works in python (in theory, anyway)