python 多執行緒 壓測 mysql
阿新 • • 發佈:2018-12-12
#!/usr/bin/env python # encoding: utf-8 #@author: 東哥加油 #@file: sthread.py #@time: 2018/9/17 17:07 import threading import time import pymysql exitFlag = 0 def get_conn48(): conn = None try: conn = pymysql.connect( host="192.168.1.3", port=3308, user="root", passwd="mysqlpass", charset="utf8", ) except Exception as err: print(err) return conn def get_data48(sql): conn = get_conn48() cur = conn.cursor() cur.execute(sql) data = cur.fetchall() return data class myThread (threading.Thread): def __init__(self, threadID, counter,member_id): threading.Thread.__init__(self) self.threadID = threadID self.name = "Thread-"+str(threadID) self.counter = counter self.member_id = member_id def run(self): print ("開始執行緒:" + self.name) print_time(self.name, self.counter,self.member_id) print ("退出執行緒:" + self.name) def print_time(threadName, counter,member_id): while counter: if exitFlag: threadName.exit() print ("%s: %s" % (threadName, time.ctime(time.time()))) conn = get_conn48() cursor = conn.cursor() try: # 執行sql語句 sql = ''' update goeses.tb_member_balance set modify_time = modify_time + 1 where member_id=%s '''%(member_id) print(sql) cursor.execute(sql) conn.commit() except: # 如果發生錯誤則回滾 conn.rollback() # 關閉資料庫連線 conn.close() counter -= 1 # 建立新執行緒 thread1 = myThread(1, 500000,1000000001) thread2 = myThread(2, 500000,1000000002) thread3 = myThread(3, 500000,1000000003) thread4 = myThread(4, 500000,1000000004) thread5 = myThread(5, 500000,1000000005) thread6 = myThread(6, 500000,1000000006) thread7 = myThread(7, 500000,1000000007) thread8 = myThread(8, 500000,1000000008) thread9 = myThread(9, 500000,1000000009) thread10 = myThread(10, 500000,1000000010) thread11 = myThread(11, 500000,1000000011) thread12 = myThread(12, 500000,1000000012) thread13 = myThread(13, 500000,1000000013) thread14 = myThread(14, 500000,1000000014) thread15 = myThread(15, 500000,1000000015) thread16 = myThread(16, 500000,1000000016) thread17 = myThread(17, 500000,1000000017) thread18 = myThread(18, 500000,1000000018) thread19 = myThread(19, 500000,1000000019) thread20 = myThread(20, 500000,1000000020) thread21 = myThread(21, 500000,1000000021) thread22 = myThread(22, 500000,1000000022) thread23 = myThread(23, 500000,1000000023) thread24 = myThread(24, 500000,1000000024) thread25 = myThread(25, 500000,1000000025) thread26 = myThread(26, 500000,1000000026) thread27 = myThread(27, 500000,1000000027) thread28 = myThread(28, 500000,1000000028) thread29 = myThread(29, 500000,1000000029) thread30 = myThread(30, 500000,1000000030) thread31 = myThread(31, 500000,1000000031) thread32 = myThread(32, 500000,1000000032) thread33 = myThread(33, 500000,1000000033) thread34 = myThread(34, 500000,1000000034) thread35 = myThread(35, 500000,1000000035) thread36 = myThread(36, 500000,1000000036) thread37 = myThread(37, 500000,1000000037) thread38 = myThread(38, 500000,1000000038) thread39 = myThread(39, 500000,1000000039) thread40 = myThread(40, 500000,1000000040) thread41 = myThread(41, 500000,1000000041) thread42 = myThread(42, 500000,1000000042) thread43 = myThread(43, 500000,1000000043) thread44 = myThread(44, 500000,1000000044) thread45 = myThread(45, 500000,1000000045) thread46 = myThread(46, 500000,1000000046) thread47 = myThread(47, 500000,1000000047) thread48 = myThread(48, 500000,1000000048) thread49 = myThread(49, 500000,1000000049) thread50 = myThread(50, 500000,1000000050) # 開啟新執行緒 thread1.start() thread2.start() thread3.start() thread4.start() thread5.start() thread6.start() thread7.start() thread8.start() thread9.start() thread10.start() thread11.start() thread12.start() thread13.start() thread14.start() thread15.start() thread16.start() thread17.start() thread18.start() thread19.start() thread20.start() thread21.start() thread22.start() thread23.start() thread24.start() thread25.start() thread26.start() thread27.start() thread28.start() thread29.start() thread30.start() thread31.start() thread32.start() thread33.start() thread34.start() thread35.start() thread36.start() thread37.start() thread38.start() thread39.start() thread40.start() thread41.start() thread42.start() thread43.start() thread44.start() thread45.start() thread46.start() thread47.start() thread48.start() thread49.start() thread50.start() thread1.join() thread2.join() thread3.join() thread4.join() thread5.join() thread6.join() thread7.join() thread8.join() thread9.join() thread10.join() thread11.join() thread12.join() thread13.join() thread14.join() thread15.join() thread16.join() thread17.join() thread18.join() thread19.join() thread20.join() thread21.join() thread22.join() thread23.join() thread24.join() thread25.join() thread26.join() thread27.join() thread28.join() thread29.join() thread30.join() thread31.join() thread32.join() thread33.join() thread34.join() thread35.join() thread36.join() thread37.join() thread38.join() thread39.join() thread40.join() thread41.join() thread42.join() thread43.join() thread44.join() thread45.join() thread46.join() thread47.join() thread48.join() thread49.join() thread50.join() print ("退出主執行緒")