mysql的链接单例模式和线程池的优劣性
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql的链接单例模式和线程池的优劣性,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4961字,纯文字阅读大概需要8分钟。
内容图文
![mysql的链接单例模式和线程池的优劣性](/upload/InfoBanner/zyjiaocheng/876/3c314cf90ccf4f07b603cc402c1ca20d.jpg)
单例模式代码
from functools import wraps import pymysql def single(cls): """实现类的单例模式""" instance_dict = dict() @wraps(cls) def single_instance(*args,**kwargs): if cls not in instance_dict: instance_dict[cls] = cls(*args,**kwargs) return instance_dict[cls] return single_instance @single class PyMySQLTest(object): def __init__(self,db_name): self.coon = None self.cursor = None self.db_name = db_name try: self.coon = pymysql.connect(host="xx",port=xx,user="xx",password="xx",database=self.db_name,charset="utf8") self.cursor = self.coon.cursor() except Exception as e: raise e def get_all(self): sql = "select cow_number from cow_info where isdelete=0" self.cursor.execute(sql) result = self.cursor.fetchall() print(result)
100个线程测试代码
from single_pool.test_mysql import mysql_test from threading import Thread import time def test(): single_instance = mysql_test.PyMySQLTest("cattle") single_instance.get_all() if __name__ == '__main__': start_time = time.time() # 创建100个线程链接实例,测试获取数据 for i in range(100): t = Thread(target=test) t.start() end_time = time.time() print(end_time-start_time)
结果分析
测试10次 时间分别为 0.032 0.031 0.037 0.031 0.032 0.030 0.032 0.030 0.035 0.032
成功的结果
0.031002044677734375 (('1002',), ('1005',), ('2007',), ('3001',), ('3004',), ('868201803000700',), ('868201803000704',), ('868201803000702',), ('868201803000696',), ('868201803000701',), ('868201803000697',), ('868201803000699',), ('868201803000689',), ('868201803000682',), ('868201803000695',), ('868201803000688',), ('868201803000681',), ('868201803000703',), ('868201803000674',), ('868201803000651',), ('180078',), ('180025',), ('130558',), ('160080',), ('160090',), ('170071',), ('170087',), ('170099',), ('170135',), ('150153',), ('160073',), ('170083',), ('150037',), ('150249',), ('160168',), ('160002',), ('170080',), ('140157',), ('160225',), ('140161',), ('140588',), ('160804',), ('140170',), ('160068',), ('16411',), ('160527',), ('180070',), ('180026',), ('180034',), ('180004',), ('1003',), ('130653',), ('160170',), ('180753',), ('180795',), ('180698',), ('180675',), ('20191001',), ('20191002',), ('140680',), ('170058',), ('170159',), ('160059',), ('170095',), ('150092',), ('20200221',), ('20200224',), ('20200226',), ('130739',), ('160169',), ('150641',), ('160829',), ('20200301',), ('2020030111',), ('202003011112',), ('170151',), ('150350',), ('180007',), ('110615',), ('160250',), ('170107',), ('150039',), ('170153',), ('110926',), ('170136',), ('160097',), ('160423',), ('160329',), ('170069',), ('170152',), ('140738',), ('110301',), ('170092',), ('160098',), ('150090',), ('180748',)) (('1002',), ('1005',), ('2007',), ('3001',), ('3004',), ('868201803000700',), ('868201803000704',), ('868201803000702',), ('868201803000696',), ('868201803000701',), ('868201803000697',), ('868201803000699',), ('868201803000689',), ('868201803000682',), ('868201803000695',), ('868201803000688',), ('868201803000681',), ('868201803000703',), ('868201803000674',), ('868201803000651',), ('180078',), ('180025',), ('130558',), ('160080',), ('160090',), ('170071',), ('170087',), ('170099',), ('170135',), ('150153',), ('160073',), ('170083',), ('150037',), ('150249',), ('160168',), ('160002',), ('170080',), ('140157',), ('160225',), ('140161',), ('140588',), ('160804',), ('140170',), ('160068',), ('16411',), ('160527',), ('180070',), ('180026',), ('180034',), ('180004',), ('1003',), ('130653',), ('160170',), ('180753',), ('180795',), ('180698',), ('180675',), ('20191001',), ('20191002',), ('140680',), ('170058',), ('170159',), ('160059',), ('170095',), ('150092',), ('20200221',), ('20200224',), ('20200226',), ('130739',), ('160169',), ('150641',), ('160829',), ('20200301',), ('2020030111',), ('202003011112',), ('170151',), ('150350',), ('180007',), ('110615',), ('160250',), ('170107',), ('150039',), ('170153',), ('110926',), ('170136',), ('160097',), ('160423',), ('160329',), ('170069',), ('170152',), ('140738',), ('110301',), ('170092',), ('160098',), ('150090',), ('180748',))(('1002',), ('1005',), ('2007',), ('3001',), ('3004',), ('868201803000700',), ('868201803000704',), ('868201803000702',), ('868201803000696',), ('868201803000701',), ('868201803000697',), ('868201803000699',), ('868201803000689',), ('868201803000682',), ('868201803000695',), ('868201803000688',), ('868201803000681',), ('868201803000703',), ('868201803000674',), ('868201803000651',), ('180078',), ('180025',), ('130558',), ('160080',), ('160090',), ('170071',), ('170087',), ('170099',), ('170135',), ('150153',), ('160073',), ('170083',), ('150037',), ('150249',), ('160168',), ('160002',), ('170080',), ('140157',), ('160225',), ('140161',), ('140588',), ('160804',), ('140170',), ('160068',), ('16411',), ('160527',), ('180070',), ('180026',), ('180034',), ('180004',), ('1003',), ('130653',), ('160170',), ('180753',), ('180795',), ('180698',), ('180675',), ('20191001',), ('20191002',), ('140680',), ('170058',), ('170159',), ('160059',), ('170095',), ('150092',), ('20200221',), ('20200224',), ('20200226',), ('130739',), ('160169',), ('150641',), ('160829',), ('20200301',), ('2020030111',), ('202003011112',), ('170151',), ('150350',), ('180007',), ('110615',), ('160250',), ('170107',), ('150039',), ('170153',), ('110926',), ('170136',), ('160097',), ('160423',), ('160329',), ('170069',), ('170152',), ('140738',), ('110301',), ('170092',), ('160098',), ('150090',), ('180748',))
失败的结果
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'xx.xx.xx.xx' (timed out)")
socket.timeout: timed out
连接池代码
#
内容总结
以上是互联网集市为您收集整理的mysql的链接单例模式和线程池的优劣性全部内容,希望文章能够帮你解决mysql的链接单例模式和线程池的优劣性所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。