最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python使用多線程查詢數(shù)據(jù)庫的實(shí)現(xiàn)示例

 更新時(shí)間:2020年08月17日 09:29:46   作者:下劃線隱患者  
這篇文章主要介紹了python使用多線程查詢數(shù)據(jù)庫的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一.背景:

         當(dāng)數(shù)據(jù)量過大時(shí),一個(gè)程序的執(zhí)行時(shí)間就會(huì)主要花費(fèi)在等待單次查詢返回結(jié)果,在這個(gè)過程中cpu無疑是處于等待io的空閑狀態(tài)的,這樣既浪費(fèi)了cpu資源,又花費(fèi)了大量時(shí)間(當(dāng)然這里主要說多線程,批量查詢不在考慮范圍,總會(huì)存在不能批量查詢的情況),在這種非密集型運(yùn)算(及大量占用cpu資源)的情況下在python中無疑運(yùn)用多線程是一個(gè)非常棒的選擇。

二.知識(shí)點(diǎn):

        數(shù)據(jù)庫連接池的運(yùn)用及優(yōu)勢,python中多線程的運(yùn)用,隊(duì)列的運(yùn)用

        數(shù)據(jù)庫連接池:限制了數(shù)據(jù)庫的連接最大個(gè)數(shù),每次連接都是可以重復(fù)使用的,當(dāng)然也可以限制每個(gè)連接的重復(fù)使用次數(shù)(這個(gè)在這里是沒必要的),需要注意的是設(shè)置的數(shù)據(jù)庫的最大連接個(gè)數(shù)最好要大于我們自己開的最大線程個(gè)數(shù),一般邏輯是每個(gè)線程占用一個(gè)數(shù)據(jù)庫連接可以使程序達(dá)到最大速度,如果小于則可能存在同時(shí)連接個(gè)數(shù)大于數(shù)據(jù)庫允許的最大連接個(gè)數(shù)的風(fēng)險(xiǎn)。使用數(shù)據(jù)庫連接池的優(yōu)勢在于,python多線程并發(fā)操作數(shù)據(jù)庫,會(huì)存在鏈接數(shù)據(jù)庫超時(shí)、數(shù)據(jù)庫連接丟失、數(shù)據(jù)庫操作超時(shí)等問題,而數(shù)據(jù)庫連接池提供線程間可共享的數(shù)據(jù)庫連接,并自動(dòng)管理連接。

       python多線程:在程序等待io的時(shí)間里調(diào)用多線程去數(shù)據(jù)庫執(zhí)行查詢操作。

       隊(duì)列:這個(gè)就是數(shù)據(jù)結(jié)構(gòu)里面的知識(shí)了,一般隊(duì)列的常用模式先進(jìn)先出隊(duì)列。(這里主要用的是隊(duì)列取一個(gè)數(shù)就少一個(gè)數(shù)的原理,其實(shí)用列表也可以實(shí)現(xiàn),他的先進(jìn)先出主要強(qiáng)調(diào)的是一個(gè)順序關(guān)系,這一點(diǎn)到?jīng)]用上,就當(dāng)是練練手了)

三.兩段代碼作比較:

數(shù)據(jù)庫的截圖:

第一段代碼:正常循環(huán)查詢并打印出執(zhí)行時(shí)間

#!/usr/bin/python
# -*- coding=utf-8 -*-
import time
import threading
import MySQLdb
import Queue
from MySQLdb.cursors import DictCursor
from DBUtils.PooledDB import PooledDB

def mysql_connection():
  host = 'localhost'
  user = 'root'
  port = 3306
  password = '123456'
  db = 'test'
  charset = 'utf8'
  limit_count = 3 # 最低預(yù)啟動(dòng)數(shù)據(jù)庫連接數(shù)量
  pool = PooledDB(MySQLdb, limit_count, maxconnections=15, host=host, user=user, port=port, passwd=password, db=db, charset=charset,
      use_unicode=True, cursorclass=DictCursor)
  return pool


start = time.time()
pool = mysql_connection()

for id in range(50):
  con = pool.connection()
  cur = con.cursor()
  sql = '''select id,name,age,weight from test where id = %s '''%id
  cur.execute(sql)
  time.sleep(0.5)
  result = cur.fetchall()
  if result:
    print('this is tread %s (%s,%s,%s,%s)'%(id,result[0]['id'],result[0]['name'],result[0]['age'],result[0]['weight']))
  else:
    print('this tread %s result is none'%id)

end = time.time() - start
print(end)

執(zhí)行結(jié)果:

第二段代碼:限制數(shù)據(jù)庫連接池最大15個(gè)連接,用隊(duì)列限制最大線程個(gè)數(shù)為10個(gè)

#!/usr/bin/python
# -*- coding=utf-8 -*-
import time
import threading
import MySQLdb
import Queue
from MySQLdb.cursors import DictCursor
from DBUtils.PooledDB import PooledDB

def mysql_connection():
  host = 'localhost'
  user = 'root'
  port = 3306
  password = '123456'
  db = 'test'
  charset = 'utf8'
  limit_count = 3 # 最低預(yù)啟動(dòng)數(shù)據(jù)庫連接數(shù)量
  pool = PooledDB(MySQLdb, limit_count, maxconnections=15, host=host, user=user, port=port, passwd=password, db=db, charset=charset,
      use_unicode=True, cursorclass=DictCursor)
  return pool

def tread_connection_db(id):
  con = pool.connection()
  cur = con.cursor()
  sql = '''select id,name,age,weight from test where id = %s '''%id
  cur.execute(sql)
  time.sleep(0.5)
  result = cur.fetchall()
  if result:
    print('this is tread %s (%s,%s,%s,%s)'%(id,result[0]['id'],result[0]['name'],result[0]['age'],result[0]['weight']))
  else:
    print('this tread %s result is none'%id)
  con.close()


if __name__=='__main__':
  start = time.time()
  #創(chuàng)建線程連接池,最大限制15個(gè)連接
  pool = mysql_connection()
  #創(chuàng)建隊(duì)列,隊(duì)列的最大個(gè)數(shù)及限制線程個(gè)數(shù)
  q=Queue.Queue(maxsize=10)
  #測試數(shù)據(jù),多線程查詢數(shù)據(jù)庫
  for id in range(50):
    #創(chuàng)建線程并放入隊(duì)列中
    t = threading.Thread(target=tread_connection_db, args=(id,))
    q.put(t)
    #隊(duì)列隊(duì)滿
    if q.qsize()==10:
      #用于記錄線程,便于終止線程
      join_thread = []
      #從對(duì)列取出線程并開始線程,直到隊(duì)列為空
      while q.empty()!=True:
        t = q.get()
        join_thread.append(t)
        t.start()
      #終止上一次隊(duì)滿時(shí)里面的所有線程
      for t in join_thread:
        t.join()
  end = time.time() - start
  print(end)

程序備注應(yīng)該還算比較清晰的哈,程序執(zhí)行結(jié)果:

四.結(jié)論:

看結(jié)果說話

到此這篇關(guān)于python使用多線程查詢數(shù)據(jù)庫的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)python 多線程查詢數(shù)據(jù)庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

易门县| 华安县| 泽普县| 桃园县| 黑水县| 香港 | 涪陵区| 伊春市| 邵阳县| 黄大仙区| 平陆县| 临安市| 清丰县| 惠来县| 阳高县| 湖口县| 环江| 搜索| 潜山县| 宁德市| 长海县| 蕲春县| 攀枝花市| 安多县| 宝鸡市| 泗水县| 日土县| 康马县| 栾城县| 波密县| 临沂市| 澜沧| 祥云县| 厦门市| 前郭尔| 乌拉特中旗| 永春县| 无极县| 宜都市| 云林县| 连州市|