Python基于多線程操作數(shù)據(jù)庫相關(guān)問題分析
本文實(shí)例分析了Python多線程操作數(shù)據(jù)庫相關(guān)問題。分享給大家供大家參考,具體如下:
python多線程并發(fā)操作數(shù)據(jù)庫,會存在鏈接數(shù)據(jù)庫超時(shí)、數(shù)據(jù)庫連接丟失、數(shù)據(jù)庫操作超時(shí)等問題。
解決方法:使用數(shù)據(jù)庫連接池,并且每次操作都從數(shù)據(jù)庫連接池獲取數(shù)據(jù)庫操作句柄,操作完關(guān)閉連接返回?cái)?shù)據(jù)庫連接池。
*連接數(shù)據(jù)庫需要設(shè)置
charset = 'utf8', use_unicode = True
,不然會報(bào)中文亂碼問題*網(wǎng)上說解決python多線程并發(fā)操作數(shù)據(jù)庫問題,連接時(shí)使用
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è)計(jì)入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- Python讀寫及備份oracle數(shù)據(jù)庫操作示例
- Python實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫并把備份數(shù)據(jù)庫郵件發(fā)送
- Python實(shí)現(xiàn)備份MySQL數(shù)據(jù)庫的方法示例
- Python腳本實(shí)現(xiàn)自動將數(shù)據(jù)庫備份到 Dropbox
- python備份文件以及mysql數(shù)據(jù)庫的腳本代碼
- python使用多線程查詢數(shù)據(jù)庫的實(shí)現(xiàn)示例
- Python基于多線程實(shí)現(xiàn)抓取數(shù)據(jù)存入數(shù)據(jù)庫的方法
- python使用多線程備份數(shù)據(jù)庫的步驟
相關(guān)文章
連接pandas以及數(shù)組轉(zhuǎn)pandas的方法
今天小編就為大家分享一篇連接pandas以及數(shù)組轉(zhuǎn)pandas的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python常見數(shù)制轉(zhuǎn)換實(shí)例分析
這篇文章主要介紹了python常見數(shù)制轉(zhuǎn)換,實(shí)例分析了二進(jìn)制、八進(jìn)制、十進(jìn)制及十六進(jìn)制之間的相互轉(zhuǎn)換技巧,需要的朋友可以參考下2015-05-05玩數(shù)據(jù)必備Python庫之numpy使用詳解
NumPy提供了許多高級的數(shù)值編程工具,如矩陣數(shù)據(jù)類型、矢量處理,以及精密的運(yùn)算庫,下面這篇文章主要給大家介紹了關(guān)于玩數(shù)據(jù)必備Python庫之numpy使用的相關(guān)資料,需要的朋友可以參考下2022-02-02python 環(huán)境搭建 及python-3.4.4的下載和安裝過程
這篇文章主要介紹了python 環(huán)境搭建 python-3.4.4的下載和安裝過程,文中給大家補(bǔ)充介紹了pycharm的基本用法,非常不錯,具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07Flask框架學(xué)習(xí)筆記之消息提示與異常處理操作詳解
這篇文章主要介紹了Flask框架學(xué)習(xí)筆記之消息提示與異常處理操作,結(jié)合實(shí)例形式分析了flask框架表單登陸消息提示、錯誤模板調(diào)用及異常處理相關(guān)操作技巧,需要的朋友可以參考下2019-08-08python中mediapipe庫踩過的坑實(shí)戰(zhàn)記錄
MediaPipe是由google制作的開源的、跨平臺的機(jī)器學(xué)習(xí)框架,可以將一些模型部署到不同的平臺和設(shè)備上使用的同時(shí),也能保住檢測速度,下面這篇文章主要給大家介紹了關(guān)于python中mediapipe庫踩過的坑的相關(guān)資料,需要的朋友可以參考下2023-04-04