某個線程要共享數據時,先將其鎖定,此時資源的狀態為“鎖定”,其他線程不能更改;直到該線程釋放資源,將資源的狀態變成“非鎖定”,其他的線程才能再次鎖定該資源。互斥鎖保證了每次只有一個線程進入寫入操作,從而保證了多線程情況下數據的正確性。
采用f_flag的方法效率低
創建鎖
mutex=threading.Lock()
鎖定
mutex.acquire([blocking])#里面可以加blocking(等待的時間)或者不加,不加就會一直等待(堵塞)
釋放
mutex.release()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import threading from threading import Thread from threading import Lock import time thnum = 0 #兩個線程都在搶著對這個鎖進行上鎖,如果有一方成功上鎖,那么導致另外一方會堵塞(一直等待),到這個鎖被解開為之 class MyThread(threading.Thread): def run( self ): mutex.acquire() for i in range ( 10000 ): global thnum thnum + = 1 print (thnum) mutex.release() def test(): global thnum mutex.acquire() #等待可以上鎖,通知而不是輪訓,沒有占用CPU for i in range ( 10000 ): thnum + = 1 print (thnum) mutex.release() #解鎖 mutex = Lock() if __name__ = = '__main__' : t = MyThread() t.start() #創建一把互斥鎖,默認是沒有上鎖的 thn = Thread(target = test) thn.start() ''''' 10000 20000 ''' |
只要一上鎖,由多任務變為單任務,相當于只有一個線程在運行。
下面的代碼相對上面加鎖的時間變短了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import threading from threading import Thread from threading import Lock import time thnum = 0 #兩個線程都在搶著對這個鎖進行上鎖,如果有一方成功上鎖,那么導致另外一方會堵塞(一直等待),到這個鎖被解開為之 class MyThread(threading.Thread): def run( self ): for i in range ( 10000 ): mutex.acquire() global thnum thnum + = 1 mutex.release() #釋放后,都開始搶,這樣上鎖的時間變短 print (thnum) def test(): global thnum for i in range ( 10000 ): mutex.acquire() thnum + = 1 mutex.release() #解鎖 print (thnum) mutex = Lock() if __name__ = = '__main__' : t = MyThread() t.start() #創建一把互斥鎖,默認是沒有上鎖的 thn = Thread(target = test) thn.start() ''''' 10000 20000 ''' |
只有必須加鎖的地方才加鎖
同步:按照預定的先后順序執行
一個運行完后,釋放下一個,下一個鎖定后運行,再釋放下一個,下一個鎖定后,運行后釋放下一個..... 釋放第一個
異步:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#異步的實現 from multiprocessing import Pool import time import os #getpid()獲取當前進程的進程號 #getppid()獲取當前進程的父進程號 def test(): #子進程 print ( "----進程池中的進程-----pid=%d,ppid=%d --" % (os.getpid(),os.getppid())) for i in range ( 3 ): print ( "-----%d----" % i) time.sleep( 1 ) return "over" #子進程執行完后返回給操作系統,返回給父進程 def test2(args): print ( "-----callback func----pid=%d" % os.getpid()) #主進程調用test2 print ( "------callback func---args=%s" % args) def main(): pool = Pool( 3 ) pool.apply_async(func = test,callback = test2) #回調 time.sleep( 5 ) #收到func進程結束后的信號后,執行回調函數test2 print ( "----主進程-pid = %d" % os.getpid()) if __name__ = = "__main__" : #main() pool = Pool( 3 ) pool.apply_async(test,callback = test2) #回調 time.sleep( 5 ) #收到func進程結束后的信號后,執行回調函數test2 print ( "----主進程-pid = %d" % os.getpid()) '''''顯示結果不太正確,應該先運行test呀,再運行test2 -----callback func----pid=7044 ------callback func---args=over ----主進程-pid = 7044 ----進程池中的進程-----pid=3772,ppid=7044 -- -----0---- -----1---- -----2---- ''' |