Python database connection Pool instance--pooleddb

Source: Internet
Author: User
Tags connection pooling

MySQL connection method without connection pooling

ImportMysqldbconn= MySQLdb.connect (host='localhost', user='Root', passwd='pwd', db='MyDB', port=3306) cur=conn.cursor () SQL="SELECT * FROM table1"R=Cur.execute (SQL) R=Cur.fetchall () cur.close () Conn.close ( )

Connection method after using the connection pool

ImportMySQLdb fromDbutils.pooleddbImportPooleddbpool= POOLEDDB (mysqldb,5,host='localhost', user='Root', passwd='pwd', db='MyDB', port=3306)#5 is the minimum number of connections in the connection poolConn= Pool.connection ()#the next time you need a database connection is to use the connection () function to get the connection.Cur=conn.cursor () SQL="SELECT * FROM table1"R=Cur.execute (SQL) R=Cur.fetchall () cur.close () Conn.close ( )
Parameters of the POOLEDDB:
1. mincached, minimum number of idle connections, if the number of idle connections is less than this, pool creates a new connection
2. maxcached, the maximum number of idle connections, if the number of idle connections is greater than this, pool closes the idle connection
3. MaxConnections, maximum number of connections,
4. Blocking, when the number of connections reaches the maximum number of connections, when the connection is requested, if this value is true, the program requesting the connection will wait until the current number of connections is less than the maximum number of connections, if this value is False, will be error,
5. Maxshared when the number of connections reaches this number, the newly requested connection will share the connection that has been allocated

In Uwsgi, each HTTP request is distributed to a process in which the number of connections configured in the connection pool is a process unit (that is, the maximum number of connections above, the number of connections in a process), and if the business The number of SQL connections required in an HTTP request is not many (most of them only need to create a connection) and the configured number of connections is not configured to be too large.
The performance improvement of the connection pool is shown in:
1. When the program creates a connection, it can be obtained from an idle connection, without having to reinitialize the connection to increase the speed of getting the connection.
2. When the connection is closed, put the connection back into the connection pool instead of the real shutdown, so you can reduce the frequent opening and closing of the connection
Ext.: http://www.cnblogs.com/Xjng/p/3437694.html

Python database connection Pool instance--pooleddb

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.