溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中怎么利用多線程操作數(shù)據(jù)庫

發(fā)布時間:2021-06-17 14:50:32 來源:億速云 閱讀:707 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)Python中怎么利用多線程操作數(shù)據(jù)庫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(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 # 最低預啟動數(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中怎么利用多線程操作數(shù)據(jù)庫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI