Python多線程 簡明例子
綜述
多線程是程序設計中的一個重要方面,尤其是在服務器Deamon程序方面。無論何種系統,線程調度的開銷都比傳統的進程要快得多。
Python可以方便地支持多線程。可以快速創建線程、互斥鎖、信號量等等元素,支持線程讀寫同步互斥。美中不足的是,Python的運行在Python 虛擬機上,創建的多線程可能是虛擬的線程,需要由Python虛擬機來輪詢調度,這大大降低了Python多線程的可用性。希望高版本的Python可以 解決這個問題,發揮多CPU的最大效率。
網上有些朋友說要獲得真正多CPU的好處,有兩種方法:
1.可以創建多個進程而不是線程,進程數和cpu一樣多。
2.使用Jython 或 IronPython,可以得到真正的多線程。
2.使用Jython 或 IronPython,可以得到真正的多線程。
閑話少說,下面看看Python如何建立線程
Python線程創建
使用threading模塊的 Thread類
類接口如下
class Thread( group=None, target=None, name=None, args=(), kwargs={}) 需要關注的參數是target和args. target 是需要子線程運行的目標函數,args是函數的參數,以tuple的形式傳遞。
以下代碼創建一個指向函數worker 的子線程
def worker(a_tid,a_account):
...
th = threading.Thread(target=worker,args=(i,acc) ) ;
...
th = threading.Thread(target=worker,args=(i,acc) ) ;
啟動這個線程
th.start()
等待線程返回
threading.Thread.join(th)
或者th.join()
如果你可以對要處理的數據進行很好的劃分,而且線程之間無須通信,那么你可以使用:創建=》運行=》回收的方式編寫你的多線程程序。但是如果線程之間需要訪問共同的對象,則需要引入互斥鎖或者信號量對資源進行互斥訪問。
下面講講如何創建互斥鎖
創建鎖
g_mutex = threading.Lock()
....
使用鎖
for ... :
#鎖定,從下一句代碼到釋放前互斥訪問
g_mutex.acquire()
a_account.deposite(1)
#釋放
g_mutex.release()
創建鎖
g_mutex = threading.Lock()
....
使用鎖
for ... :
#鎖定,從下一句代碼到釋放前互斥訪問
g_mutex.acquire()
a_account.deposite(1)
#釋放
g_mutex.release()
最后,模擬一個公交地鐵IC卡繳車費的多線程程序
有10個讀卡器,每個讀卡器收費器每次扣除用戶一塊錢進入總賬中,每讀卡器每天一共被刷10000000次。賬戶原有100塊。所以最后的總賬應該為10000100。先不使用互斥鎖來進行鎖定(注釋掉了鎖定代碼),看看后果如何。
import time,datetime import threading def worker(a_tid,a_account): global g_mutex print("Str " , a_tid, datetime.datetime.now() ) for i in range(1000000): #g_mutex.acquire() a_account.deposite(1) #g_mutex.release() print("End " , a_tid , datetime.datetime.now() ) class Account: def __init__ (self, a_base ): self.m_amount=a_base def deposite(self,a_amount): self.m_amount+=a_amount def withdraw(self,a_amount): self.m_amount-=a_amount if __name__ == "__main__": global g_mutex count = 0 dstart = datetime.datetime.now() print("Main Thread Start At: ", dstart) #init thread_pool thread_pool = [] #init mutex g_mutex = threading.Lock() # init thread items acc = Account(100) for i in range(10): th = threading.Thread(target=worker,args=(i,acc) ) ; thread_pool.append(th) # start threads one by one for i in range(10): thread_pool[i].start() #collect all threads for i in range(10): threading.Thread.join(thread_pool[i]) dend = datetime.datetime.now() print("count=", acc.m_amount) print("Main Thread End at: ", dend, " time span ", dend-dstart)
注意,先不用互斥鎖進行臨界段訪問控制,運行結果如下:
從結果看到,程序確實是多線程運行的。但是由于沒有對對象Account進行互斥訪問,所以結果是錯誤的,只有3434612,比原預計少了很多。
打開鎖后:
這次可以看到,結果正確了。運行時間比不進行互斥多了很多,不過這也是同步的代價。
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!