python的互斥鎖,加鎖、同步機制、異步通信

某個線程要共享數據時,先將其鎖定,此時資源的狀態爲“鎖定”,其他線程不能更改;直到該線程釋放資源,將資源的狀態變成“非鎖定”,其他的線程才能再次鎖定該資源。互斥鎖保證了每次只有一個線程進入寫入操作,從而保證了多線程情況下數據的正確性。


採用f_flag的方法效率低(上一篇文章最後)


創建鎖

mutex=threading.Lock()

鎖定

mutex.acquire([blocking])#裏面可以加blocking(等待的時間)或者不加,不加就會一直等待(堵塞)

釋放

mutex.release()


1、只要一上鎖,由多任務變爲單任務,相當於只有一個線程在運行

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

'''

2、下面的代碼相對上面加鎖的時間變短了

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

'''


3、異步實現

同步:按照預定的先後順序執行

一個運行完後,釋放下一個,下一個鎖定後運行,再釋放下一個,下一個鎖定後,運行後釋放下一個.....  釋放第一個

異步:

#異步的實現

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----

'''


--------------------- 

作者:哈嘿哈嘿搬磚嘍 

來源:CSDN 

原文:https://blog.csdn.net/yanhuatangtang/article/details/75316644 

版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章