英文:
The difference between asyncpg and psycopg2
问题
为什么在Python中存在asyncpg
库,如果PostgreSQL已经支持同时进行多个连接?能有人解释一下它们的区别,以及在哪些情况下应该使用哪个库?
> 对我来说,重要的是找出它们之间的区别,因为我计划创建有很多async
函数的discord机器人,如果psycopg2
像time.sleep()
在async
函数中一样阻塞脚本,那对我就不合适。
英文:
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.
答案1
得分: 0
为什么Python中存在asyncpg库?
库存在是因为有人想要编写它们。没有人负责告诉别人他们不能编写新的库。因此,存在并不是必要性的证据。但是...
PostgreSQL已经支持同时连接多个连接吗?
但是,你如何利用它呢?psycopg2的主要方法是阻塞的。当你发送一个查询时,它会阻塞直到结果准备好并被获取。你可以打开另一个连接,但你不能轻松地控制回到那个其他连接以执行某些操作。有一些解决这个限制的方法,但它们被作为事后添加,而asyncpg提供了更符合Python中其他异步编程方式的更好的方法(理论上是这样)。
英文:
> 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论