Python base 9.5 database connection Pool

Source: Internet
Author: User
Tags connection pooling

I. Database connection poolpython programming can use MYSQLDB for database connection and operations such as query, insert, UPDATE, but each time the MySQL database request, are independent to request access, quite a waste of resources, and access to a certain amount of book power, Will have a big impact on MySQL performance. Therefore, in actual use, the database connection pooling technology is usually used to access the database to achieve the purpose of resource reuse.  Python database Connection pool package Dbutils:Dbutils is a set of Python database connection pool packages and allows thread-safe wrapping of non-thread-safe database interfaces. Dbutils from Webware for PthonThe Dbutils offers two external interfaces:* PERSISTENTDB: Provides a thread-specific database connection and automatically manages the connection. *POOLEDDB: Provides a database connection that can be shared between threads and automatically manages connections. :https://pypi.python.org/pypi/DBUtils/Download and unzip, install using the Python setup.py install command or use: Pip install dbutils#/usr/bin/python#-*-coding:utf-8-*-# @Time: 2017/11/22 15:43# @Auther: Liuzhenchuan# @File: Database connection pool. pyimport mysqldb from dbutils.pooleddb import pooleddb db_config= { ' host ':' 192.168.10.199 ', ' Port ':3306, ' user ':' root ', ' passwd ':' 123123 ', ' db ':' python ', ' charset ':' UTF8 '}pool = pooleddb (creator=mysqldb,mincached=5,blocking=True, **db_ Config) if __name__ = = ' __main__ ': CNX = pool.connection ()cus = cnx.cursor ()SQL = ' select *from test; ' try: Cus.execute (SQL)result = Cus.fetchall () Print result cus.close ()Cnx.commit except Exception as e: raise e finally: cnx.close ()>>>(100L,), (99L,), (95L,), (95L,), (98L,), (97L,), (96L,), (95L,), (100L,), (101L,), (102L,), (103L,), (104L,), (105L,) , (106L,), (107L,), (108L,), (109L,), (110L,), (111L,), (112L,), (113L,), (114L,), (115L,), (116L,),, (117L,), (118L,), (11 9L,), (120L,), (121L,), (122L,), (123L,), (124L,), (125L,), (126L,), (127L,), (128L,), (129L,))parameter Description:pool = pooleddb (mysqldb,5, **db_config) #5 the minimum number of connections in the connection poolCNX = pool.connection () # Every time a database connection is needed, it's OK to use the connection () function to get the connection.cus = cnx.cursor ()cus = cnx.cursor ()SQL = ' select *from test; ' try: Cus.execute (SQL)result = Cus.fetchall () Print result cus.close ()Cnx.commitparameters of the POOLEDDB:1.mincached, minimum number of idle connections, if the number of idle connections is less than this, pool creates a new connection2.maxcached, the maximum number of idle connections, if the number of idle connections is greater than this, pool closes the idle connection3.maxconnections, maximum number of connections. 4.blocking, when the number of links to the maximum number of links, when the connection is requested, if the value is true, the request to connect the program will wait until the current number of connections is less than the maximum number of connections, if this value is Flase, will be an error5.maxshared When the number of connections reaches this number, the new requested link will share the link that has been assigned. in Uwsgi, each httpd request is distributed to a process, and the number of links configured in the connection pool is a process unit (that is, the maximum number of connections above, which is the number of links in a process), and if the business The number of SQL connections required in a HTTPD request is not many (most of them just need to create a connection) and the configured number of links does not need to be too large. Connection Pool Performance Improvements change now:1. When a program creates a connection, it can be obtained from an idle link, without having to reinitialize the connection to increase the speed of getting the link2. When the connection is closed, put the link back into the connection pool instead of actually shutting it down, so you can reduce the frequent opening and closing of the connection

Python base 9.5 database connection Pool

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.