文章從簡書轉入,只因它已不再是以前的簡書
If you shut the door to all errors, truth will be shut out.
你如果拒絕面對錯誤,真相也會被擋在門外。
多線程類似於同時執行多個不同程序,多線程運行有如下優點:
- 使用線程可以把佔據長時間的程序中的任務放到後臺去處理
- 用戶界面可以更加吸引人,這樣比如用戶點擊了一個按鈕去觸發某些事件的處理,可以彈出一個進度條來顯示處理的進度
- 程序的運行速度可能加快
- 在一些等待的任務實現上如用戶輸入、文件讀寫和網絡收發數據等,線程就比較有用了。在這種情況下我們可以釋放一些珍貴的資源如內存佔用等等
線程和進程
- 每個獨立的線程有一個程序運行的入口、順序執行序列和程序的出口
- 線程不能夠獨立執行,必須依存在應用程序中,由應用程序提供多個線程執行控制。
線程是操作系統能夠進行運算調度的最小單位。它被包含在進程之中,是進程中的實際運作單位。一條線程指的是進程中一個單一順序的控制流,一個進程中可以併發多個線程,每條線程並行執行不同的任務
Python3 線程中常用的兩個模塊爲:
- _thread
- threading (推薦使用)
_thread 的簡單使用
thread
模塊已被廢棄。用戶可以使用 threading
模塊代替。所以,在 Python3 中不能再使用"thread"
模塊。爲了兼容性,Python3 將 thread
重命名爲 "_thread"
。
函數式:調用 _thread 模塊中的start_new_thread()函數來產生新線程。語法如下:
_thread.start_new_thread ( function, args[, kwargs] )
參數說明:
- function
- 線程函數。
- args
- 傳遞給線程函數的參數,他必須是個tuple類型。
- kwargs
- 可選參數。
# 引入線程模塊
import _thread
# 時間模塊,用於輔助操作
import time
# 爲線程定義一個函數
def print_time(threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print ("%s: %s" % ( threadName, time.ctime(time.time()) ))
# 創建兩個線程
try:
_thread.start_new_thread( print_time, ("Thread-1", 2, ) )
_thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
print ("Error: 無法啓動線程")
# 讓程序一直運行
while 1:
pass
輸出結果:
Thread-1: Tue Nov 28 17:26:46 2017
Thread-2: Tue Nov 28 17:26:48 2017
Thread-1: Tue Nov 28 17:26:48 2017
Thread-1: Tue Nov 28 17:26:50 2017
Thread-2: Tue Nov 28 17:26:52 2017
Thread-1: Tue Nov 28 17:26:52 2017
Thread-1: Tue Nov 28 17:26:54 2017
Thread-2: Tue Nov 28 17:26:56 2017
Thread-2: Tue Nov 28 17:27:00 2017
Thread-2: Tue Nov 28 17:27:04 2017
總結:線程1 和線程2 都在運行
threading 的簡單使用
_thread
提供了低級別的、原始的線程以及一個簡單的鎖,它相比於
threading` 模塊的功能還是比較有限的。
threading
模塊除了包含 _thread
模塊中的所有方法外,還提供的其他方法:
- threading.currentThread()
: 返回當前的線程變量。
- threading.enumerate()
: 返回一個包含正在運行的線程的list。正在運行指線程啓動後、結束前,不包括啓動前和終止後的線程。
- threading.activeCount()
: 返回正在運行的線程數量,與len(threading.enumerate())
有相同的結果。
除了使用方法外,線程模塊同樣提供了Thread類來處理線程,Thread
類提供了以下方法:
- run()
: 用以表示線程活動的方法。
- start()
:啓動線程活動。
- join([time])
: 等待至線程中止。這阻塞調用線程直至線程的join() 方法被調用中止-正常退出或者拋出未處理的異常-或者是可選的超時發生。
- isAlive()
: 返回線程是否活動的。
- getName()
: 返回線程名。
- setName()
: 設置線程名。
使用 threading 模塊創建線程
我們可以通過直接從 threading.Thread
繼承創建一個新的子類,並實例化後調用 start()
方法啓動新線程,即它調用了線程的 run()
方法:
import threading
import time
# 終止符
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
# threading自帶函數,用以表示線程活動的方法
def run(self):
print ("開始線程:" + self.name)
print_time(self.name, self.counter, 5)
print ("退出線程:" + self.name)
# 爲線程準備的方法,用於在線程中執行
def print_time(threadName, delay, counter):
while counter:
if exitFlag:
threadName.exit()
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
# 創建新線程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# 開啓新線程
thread1.start()
thread2.start()
# 等待至線程中止
thread2.join()
thread1.join()
# 所有線程結束,後執行此操作
print ("退出主線程")
輸出結果:
開始線程:Thread-1
開始線程:Thread-2
Thread-1: Tue Nov 28 17:33:22 2017
Thread-1: Tue Nov 28 17:33:23 2017
Thread-2: Tue Nov 28 17:33:23 2017
Thread-1: Tue Nov 28 17:33:24 2017
Thread-1: Tue Nov 28 17:33:25 2017
Thread-2: Tue Nov 28 17:33:25 2017
Thread-1: Tue Nov 28 17:33:26 2017
退出線程:Thread-1
Thread-2: Tue Nov 28 17:33:27 2017
Thread-2: Tue Nov 28 17:33:29 2017
Thread-2: Tue Nov 28 17:33:31 2017
退出線程:Thread-2
退出主線程
總結:執行 start() 方法開啓線程;執行 exit()方法退出線程;執行 join() 加入線程池,等待其完成後,執行後續操作。
線程同步
如果多個線程共同對某個數據修改,則可能出現不可預料的結果,爲了保證數據的正確性,需要對多個線程進行同步
線程鎖(互斥鎖Mutex)
使用 Thread
對象的 Lock
和 Rlock
可以實現簡單的線程同步,這兩個對象都有 acquire
方法和 release
方法,對於那些需要每次只允許一個線程操作的數據,可以將其操作放到 acquire
和 release
方法之間。
不使用線程同步(不加鎖)
示例:
import time, threading
# 假定這是你的銀行存款:
balance = 0
def change_it(n):
# 先存後取,結果應該爲0:
global balance
balance = balance + n
balance = balance - n
def run_thread(n):
for i in range(100000):
change_it(n)
t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)
輸出結果:我這裏是8, 不同的機器輸出不一樣,結果不固定
-8
使用線程同步(加鎖)
import time, threading
# 假定這是你的銀行存款:
balance = 0
# 定義鎖對象
lock = threading.Lock()
def change_it(n):
# 先存後取,結果應該爲0:
global balance
balance = balance + n
balance = balance - n
def run_thread(n):
for i in range(100000):
# 先要獲取鎖:
lock.acquire()
try:
# 放心地改吧:
change_it(n)
finally:
# 改完了釋放鎖:
lock.release()
t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)
輸出結果永遠是 0
Semaphore(信號量)
互斥鎖 同時只允許一個線程更改數據,而Semaphore是同時允許一定數量的線程更改數據
import threading,time
def run(n):
semaphore.acquire()
time.sleep(1)
print("run the thread: %s\n" %n)
semaphore.release()
if __name__ == '__main__':
num= 0
semaphore = threading.BoundedSemaphore(5) #最多允許5個線程同時運行
for i in range(20):
t = threading.Thread(target=run,args=(i,))
t.start()
while threading.active_count() != 1:
pass #print threading.active_count()
else:
print('----all threads done---')
print(num)
run the thread: 4
run the thread: 2
run the thread: 3
run the thread: 0
run the thread: 1
run the thread: 5
run the thread: 9
run the thread: 8
run the thread: 6
run the thread: 7
run the thread: 11
run the thread: 13
run the thread: 10
run the thread: 12
run the thread: 14
run the thread: 15
run the thread: 19
run the thread: 17
run the thread: 18
run the thread: 16
----all threads done---
0
總結:每次開啓五個線程,順序隨機
PS: BoundedSemaphore() 改成 1,就變成了單線程,順序執行
Timer
讓一個方法在子線程裏延遲執行,time.sleep() 是在主線程睡眠
代碼示例:
import threading
def hello():
print("hello, world")
t = threading.Timer(30.0, hello)
t.start()
# 30 秒後, "hello, world" 將會被打印
線程優先級隊列( Queue)
Python 的 Queue 模塊中提供了同步的、線程安全的隊列類,包括FIFO(先入先出)隊列Queue,LIFO(後入先出)隊列LifoQueue,和優先級隊列 PriorityQueue
Queue 模塊中的常用方法:
- Queue.qsize() 返回隊列的大小
- Queue.empty() 如果隊列爲空,返回True,反之False
- Queue.full() 如果隊列滿了,返回True,反之False
- Queue.full 與 maxsize 大小對應
- Queue.get([block[, timeout]])獲取隊列,timeout等待時間
- Queue.get_nowait() 相當Queue.get(False)
- Queue.put(item) 寫入隊列,timeout等待時間
- Queue.put_nowait(item) 相當Queue.put(item, False)
- Queue.task_done() 在完成一項工作之後,Queue.task_done()函數向任務已經完成的隊列發送一個信號
- Queue.join() 實際上意味着等到隊列爲空,再執行別的操作
實例:
import queue
import threading
import time
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print ("開啓線程:" + self.name)
process_data(self.name, self.q)
print ("退出線程:" + self.name)
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print ("%s processing %s" % (threadName, data))
else:
queueLock.release()
time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = queue.Queue(10)
threads = []
threadID = 1
# 創建新線程
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# 填充隊列
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# 等待隊列清空
while not workQueue.empty():
pass
# 通知線程是時候退出
exitFlag = 1
# 等待所有線程完成
for t in threads:
t.join()
print ("退出主線程")
輸出結果:
開啓線程:Thread-1
開啓線程:Thread-2
開啓線程:Thread-3
Thread-1 processing One
Thread-2 processing Two
Thread-3 processing Three
Thread-1 processing Four
Thread-2 processing Five
退出線程:Thread-3
退出線程:Thread-2
退出線程:Thread-1
退出主線程
總結:使用隊列後, 線程是先進後出,即:LIFO