了解python中的数据库连接池

时间:2014-10-08 21:33:39

标签: python database multithreading connection-pooling fork

我不确定我是否理解python中数据库连接池的用例(例如:psycopg2.pool和mysql.connector.pooling)。在我看来,并行性通常是在python中使用多进程而不是多线程方法实现的,因为GIL,并且在多进程情况下这些池不是非常有用,因为每个进程将初始化它自己的池并且一次只能运行一个线程。它是否正确?在使用多个进程时是否存在共享数据库连接池的策略,如果没有,那么池的用途是否仅限于多线程python应用程序,还是存在其他可以使用它们的场景?

1 个答案:

答案 0 :(得分:5)

基思,

你走在正确的轨道上。正如S.O帖子中提到的那样“Accessing a MySQL connection pool from Python multiprocessing,”:

Making a seperate pool for each process is redundant and opens up way
too many connections.

查看另一个S.O帖子“What is the best solution for database connection pooling in python?”,它包含python中的示例池解决方案。如果您的应用程序要成为多线程,本文还讨论了db-pooling的局限性:

Making your own connection pool is a BAD idea if your app ever decides to start using 
multi-threading. Making a connection pool for a multi-threaded application is much 
more complicated than one for a single-threaded application. You can use something 
like PySQLPool in that case.

在python中实现db pool的方面,如“Application vs Database Resident Connection Pool”中所述,如果你的数据库支持它,最好的实现将涉及:

Let connection pool be maintained and managed by database itself 
(example: Oracle's DRCP) and calling modules just ask connections from the connection 
broker described by Oracle DRCP.

如果您有任何疑问,请与我们联系!