2014-10-08 86 views
1

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

回答

3

Keith,

你在正确的轨道上。正如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-池,如果你的应用程序要成为多线程:

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池,作为中提到的“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. 

如果您有任何问题,请让我知道!

+0

感谢您的解释 – Keith 2014-10-09 20:59:39