2014-10-08 34 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