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

Python基于多線程操作數(shù)據(jù)庫相關(guān)問題分析

 更新時間:2018年07月11日 08:41:28   作者:CyborgLin  
這篇文章主要介紹了Python基于多線程操作數(shù)據(jù)庫相關(guān)問題,結(jié)合實例形式分析了Python使用數(shù)據(jù)庫連接池并發(fā)操作數(shù)據(jù)庫避免超時、連接丟失相關(guān)實現(xiàn)技巧,需要的朋友可以參考下

本文實例分析了Python多線程操作數(shù)據(jù)庫相關(guān)問題。分享給大家供大家參考,具體如下:

python多線程并發(fā)操作數(shù)據(jù)庫,會存在鏈接數(shù)據(jù)庫超時、數(shù)據(jù)庫連接丟失、數(shù)據(jù)庫操作超時等問題。

解決方法:使用數(shù)據(jù)庫連接池,并且每次操作都從數(shù)據(jù)庫連接池獲取數(shù)據(jù)庫操作句柄,操作完關(guān)閉連接返回數(shù)據(jù)庫連接池。

*連接數(shù)據(jù)庫需要設(shè)置charset = 'utf8', use_unicode = True,不然會報中文亂碼問題

*網(wǎng)上說解決python多線程并發(fā)操作數(shù)據(jù)庫問題,連接時使用self.conn.ping(True)(檢查并保持長連接),但是我這邊親測無法解決,建議還是使用數(shù)據(jù)庫連接池

python多線程代碼:

import threading
class MyThread(threading.Thread):
  def __init__(self, name, count, exec_object):
    threading.Thread.__init__(self)
    self.name = name
    self.count = count
    self.exec_object = exec_object
  def run(self):
    while self.count >= 0:
      count = count - 1
      self.exec_object.execFunc(count)
thread1 = MyThread('MyThread1', 3, ExecObject())
thread2 = MyThread('MyThread2', 5, ExecObject())
thread1.start()
thread2.start()
thread1.join() # join方法 執(zhí)行完thread1的方法才繼續(xù)主線程
thread2.join() # join方法 執(zhí)行完thread2的方法才繼續(xù)主線程
# 執(zhí)行順序 并發(fā)執(zhí)行thread1 thread2,thread1和thread2執(zhí)行完成才繼續(xù)執(zhí)行主線程
# ExecObject類是自定義數(shù)據(jù)庫操作的業(yè)務(wù)邏輯類
#
########join方法詳解########
thread1 = MyThread('MyThread1', 3, ExecObject())
thread2 = MyThread('MyThread2', 5, ExecObject())
thread1.start()
thread1.join() # join方法 執(zhí)行完thread1的方法才繼續(xù)主線程
thread2.start()
thread2.join() # join方法 執(zhí)行完thread2的方法才繼續(xù)主線程
# 執(zhí)行順序 先執(zhí)行thread1,執(zhí)行完thread1再執(zhí)行thread2,執(zhí)行完thread2才繼續(xù)執(zhí)行主線程

mysql數(shù)據(jù)庫連接池代碼:

import MySQLdb
from DBUtils.PooledDB import PooledDB
class MySQL:
  host = 'localhost'
  user = 'root'
  port = 3306
  pasword = ''
  db = 'testDB'
  charset = 'utf8'
  pool = None
  limit_count = 3 # 最低預(yù)啟動數(shù)據(jù)庫連接數(shù)量
  def __init__(self):
    self.pool = PooledDB(MySQLdb, self.limit_count, host = self.host, user = self.user, passwd = self.pasword, db = self.db,
      port = self.port, charset = self.charset, use_unicode = True)
  def select(self, sql):
    conn = self.pool.connection()
    cursor = conn.cursor()
    cursor.execute(sql)
    result = cursor.fetchall()
    cursor.close()
    conn.close()
    return result
  def insert(self, table, sql):
    conn = self.pool.connection()
    cursor = conn.cursor()
    try:
      cursor.execute(sql)
      conn.commit()
      return {'result':True, 'id':int(cursor.lastrowid)}
    except Exception as err:
      conn.rollback()
      return {'result':False, 'err':err}
    finally:
      cursor.close()
      conn.close()

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫程序設(shè)計入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論

江达县| 延边| 神农架林区| 罗源县| 义马市| 南投市| 旺苍县| 启东市| 会宁县| 南陵县| 略阳县| 涪陵区| 民勤县| 浮山县| 新丰县| 永平县| 嵊州市| 德兴市| 东辽县| 芜湖县| 雷州市| 广昌县| 彭山县| 河池市| 中江县| 宕昌县| 云南省| 剑阁县| 惠来县| 宝丰县| 潍坊市| 二连浩特市| 永济市| 亚东县| 德庆县| 淮安市| 祁连县| 昌乐县| 历史| 资中县| 谷城县|