If you think it takes too long to open a thread to request a database, you can use the python database connection pool to improve the shortcomings. The following is a detailed introduction of the article, you can use our article to have a better understanding of the python database connection pool.
Yesterday I tested how much it will take to open 500 threads to request the database. That is, the efficiency of initiating so many threads at the same time. So I thought whether using the database connection pool technology can significantly improve such connection operations. After you finish the sorting, You need to test the data: the efficiency of frequently establishing and disabling database connections and the Performance Comparison Between the connection pool!
1. DBUtils module Learning
DBUtils is actually a Python package containing two sub-modules, one for connecting DB-API 2 modules, and the other for connecting typical PyGreSQL modules. Global DB-API 2 variable
- SteadyDB.py
Used for stable database connection
- PooledDB.py
Connection Pool
- PersistentDB.py
Maintain continuous database connections)
- SimplePooledDB.py
Simple connection pool PS: extract the DB-API out of it
- <!--[if !vml]--><!--[endif]-->
The installation provides basic services for the two modules of the top-level module, PersistentDB and PooledDB.
DBUtils. PersistentDB achieves a strong, thread-safe, stubborn database connection, using the DB-API 2 module. As shown in the use of PersistentDB connection layer steps: DBUtils. PooledDB implements a strong, thread-safe, cached, reusable database connection, using any DB-API 2 module. For example, the workflow for using PooledDB is shown as follows:
Currently, we have two modules to choose from: PersistentDB and PooledDB. They are designed to reuse database connections to improve performance and maintain database stability.
- python setup.py install
Specific module learning:
DBUtils. SimplePooledDB is a very simple database connection pool implementation. He lacks many functions than the perfect PooledDB module. DBUtils. SimplePooledDB is essentially similar to the component of the MiscUtils. DBPool Webware. You can think of it as a demo program.
DBUtils. SteadyDB is a module that achieves "strong" database connection, based on the original connection established in DB-API 2. A "strong" connection means that the connection will be reconnected after the connection is closed or when the operation limit is applied. A typical example is when the database is restarted, and your program is still running and needs to access the database, or when your program connects to the remote database behind a firewall, the firewall is lost when it is restarted.
Generally, you do not need to use SteadyDB directly.