Python database connection pool examples

Source: Internet
Author: User

The following describes how to use the Python database connection pool in a multi-threaded environment. If you are interested in the steps related to the application of the Python database connection pool in its specific solution, you can click the following article to learn about it.

Example:

 
 
  1. #-*-coding:utf-8-*-  
  2. import threading,time,datetime  
  3. import MySQLdb  
  4. from DBUtils import PooledDB  
  5. pool = PooledDB.PooledDB(MySQLdb,100,50,100,490,False,
    host='localhost',user='root',passwd='321',db='test',
    charset='utf8')   

By default, the system creates 100 database connections. Check and find that there are 100 Databases

 
 
  1. class MyThread(threading.Thread):  
  2. def __init__(self,threadName):  
  3. self.conn = pool.connection()    

Extract from database connection pool directly

 
 
  1. threading.Thread.__init__(self,name=threadName)  
  2. def run(self):  
  3. cursor=self.conn.cursor()  
  4. print "hello--->",self.getName()  
  5. file_objct = open('8.txt','a+')  
  6. file_objct.write(self.getName()+'\n')  
  7. file_objct.close()  
  8. #cursor.execute("call loaddate();")  
  9. #self.conn.commit()  
  10. time.sleep(10)  
  11. def __del__(self):  
  12. self.conn.close()  
  13. self.conn = None   
  14. for i in range(5):  
  15. obj = MyThread(str(i))  
  16. obj.start()  
  17.  

If I open 480 threads, the database will display 480 connections! Maxconnections: Maximum number of allowed connections (0 by default indicates no limit). If I adjust the Code as follows:

 
 
  1. #-*-coding:utf-8-*-  
  2. import threading,time,datetime  
  3. import MySQLdb  
  4. from DBUtils import PooledDB  
  5. pool = PooledDB.PooledDB(MySQLdb,100,50,100,400,False,
    host='localhost',user='root',passwd='321',db='test',
    charset='utf8')  
  6. class MyThread(threading.Thread):  
  7. def __init__(self,threadName):  
  8. self.conn = pool.connection()   
  9. threading.Thread.__init__(self,name=threadName)  
  10. def run(self):  
  11. cursor=self.conn.cursor()  
  12. print "hello--->",self.getName()  
  13. file_objct = open('8.txt','a+')  
  14. file_objct.write(self.getName()+'\n')  
  15. file_objct.close()  
  16. #cursor.execute("call loaddate();")  
  17. #self.conn.commit()  
  18. time.sleep(10)  
  19. def __del__(self):  
  20. self.conn.close()  
  21. self.conn = None   
  22. for i in range(402):  
  23. obj = MyThread(str(i))  
  24. obj.start()   

The maximum number of connection pools is 400.

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.