Python数据库连接池相关示例详细介绍

开发 后端
以下的内容主要是介绍Python数据库连接池应用于多线程环境中使用的具体方案的具体介绍。你就可以点击以下的文章,对其进行了解。

下面的内容主要是介绍Python数据库连接池应用于多线程环境中使用的具体方案的具体介绍。如果你对Python数据库连接池在其具体方案应用的相关步骤感兴趣的话,你就可以点击以下的文章,对其进行了解。

示例:

  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')   

默认打开的时候就创建了100个数据库连接。检查发现果然数据库中有100个

 

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

 

直接从数据库连接池中提取

 

  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.  

 

 

 

如果我开480个线程的话 数据库显示的正是480个连接!maxconnections: ***允许连接数量(缺省值 0 代表不限制)如果我现在将代码调整如下:

 

  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()   

 

连接池***的数目才400 。

【编辑推荐】

  1. Python复制文件的实际操作方案与代码详解
  2. Python 文件相关实际应用方案与代码详解
  3. Python open读写文件的实际应用方案详解
  4. Python socket编程在具体应用中前两个步骤的介绍
  5. Python Library中Condition的具体操作方案
责任编辑:佚名 来源: 互联网
相关推荐

2009-06-24 07:53:47

Hibernate数据

2010-03-18 15:09:15

python数据库连接

2017-06-22 14:13:07

PythonMySQLpymysqlpool

2009-08-10 17:34:42

C#数据库连接池

2009-06-16 09:25:31

JBoss配置

2019-11-27 10:31:51

数据库连接池内存

2021-08-12 06:52:01

.NET数据库连接池

2018-10-10 14:27:34

数据库连接池MySQL

2020-04-30 14:38:51

数据库连接池线程

2011-08-09 15:25:14

线程池数据库连接池

2011-07-29 15:11:42

WeblogicOracle数据库连接

2011-05-19 09:53:33

数据库连接池

2009-07-17 13:32:49

JDBC数据库

2009-07-29 09:33:14

ASP.NET数据库连

2018-01-03 14:32:32

2009-12-29 11:15:45

ADO数据库

2011-06-23 09:00:04

QT QODBC 数据库

2009-06-26 14:41:48

ADO.NET

2021-07-07 14:20:15

高并发服务数据库

2009-01-15 09:02:27

JMXJBossJMX监控
点赞
收藏

51CTO技术栈公众号