MySQL data connection pool under Python--Singleton

Source: Internet
Author: User

#Coding:utf-8ImportThreadingImportPymysql fromDbutils.pooleddbImportPOOLEDDB fromApp.common.file_configImportGet_configclassDbpool (object): _instance_lock=Threading. Lock ()def __init__(self):if  notHasattr (Dbpool,"Pool"): Dbpool.mysql_pool ()Else:            Pass    def __new__(CLS, *args, * *Kwargs):if  notHasattr (Dbpool,"_instance"): With Dbpool._instance_lock:if  notHasattr (Dbpool,"_instance"): Dbpool._instance= object.__new__(CLS, *args, * *Kwargs)returndbpool._instance @staticmethoddefMysql_pool (): Host= Get_config ('Database','Mysql_host') Port= Int (Get_config ('Database','Mysql_port')) User= Get_config ('Database','Mysql_username') passwd= Get_config ('Database','Mysql_password') DB= Get_config ('Database','mysql_db') Dbpool.pool=Pooleddb (creator=pymysql,#modules that use linked databasesmincached=2,#at least 0 of the free links created in the link pool are not created when initializingMaxcached=6,#most idle links in the link pool, 0 and none are not limitedMaxshared=3,            #The maximum number of links shared in a linked pool, 0 and none means sharing all. PS: Useless, because Pymysql and mysqldb modules such as threadsafety are 1, all values regardless of set to how much, _maxcached forever is 0, so forever is all links are shared. Maxconnections=8,#the maximum number of connections allowed for a connection pool, 0 and none means no limit on the number of connectionsBlocking=true,#If there are no available connections in the connection pool, wait is blocked. True, wait, False, not wait and then errorMaxusage=none,#the maximum number of times a link is reused, none means unlimitedSetsession=none,#a list of commands to execute before starting the session. such as: ["Set Datestyle to ...", "Set time zone ..."]            #Ping the MySQL server to check if the service is available. # for example: 0 = None = never, 1 = default = Whenever it is requested, 2 = When a cursor is created, 4 = When a query is executed, 7 = Alwayshost=Host, Port=Port, User=User, passwd=passwd, DB=db, Use_unicode=False, CharSet='UTF8'        )

MySQL data connection pool under Python--Singleton

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.