python互斥鎖、加鎖、同步機制、異步通信知識總結
某個線程要共享數(shù)據(jù)時,先將其鎖定,此時資源的狀態(tài)為“鎖定”,其他線程不能更改;直到該線程釋放資源,將資源的狀態(tài)變成“非鎖定”,其他的線程才能再次鎖定該資源?;コ怄i保證了每次只有一個線程進入寫入操作,從而保證了多線程情況下數(shù)據(jù)的正確性。
采用f_flag的方法效率低
創(chuàng)建鎖
mutex=threading.Lock()
鎖定
mutex.acquire([blocking])#里面可以加blocking(等待的時間)或者不加,不加就會一直等待(堵塞)
釋放
mutex.release()
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()
#創(chuàng)建一把互斥鎖,默認是沒有上鎖的
thn=Thread(target=test)
thn.start()
'''''
10000
20000
'''
只要一上鎖,由多任務變?yōu)閱稳蝿?相當于只有一個線程在運行。
下面的代碼相對上面加鎖的時間變短了
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()
#創(chuàng)建一把互斥鎖,默認是沒有上鎖的
thn=Thread(target=test)
thn.start()
'''''
10000
20000
'''
只有必須加鎖的地方才加鎖
同步:按照預定的先后順序執(zhí)行
一個運行完后,釋放下一個,下一個鎖定后運行,再釋放下一個,下一個鎖定后,運行后釋放下一個..... 釋放第一個
異步:
#異步的實現(xiàn)
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" #子進程執(zhí)行完后返回給操作系統(tǒng),返回給父進程
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進程結束后的信號后,執(zhí)行回調函數(shù)test2
print("----主進程-pid = %d"%os.getpid())
if __name__=="__main__":
#main()
pool=Pool(3)
pool.apply_async(test,callback=test2)#回調
time.sleep(5)#收到func進程結束后的信號后,執(zhí)行回調函數(shù)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----
'''
相關文章
Django之全局使用request.user.username的實例詳解
這篇文章主要介紹了Django之全局使用request.user.username的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
一篇文章徹底搞懂Python中可迭代(Iterable)、迭代器(Iterator)與生成器(Generator)的概念
這篇文章主要給大家介紹了如何通過一篇文章徹底搞懂Python中可迭代(Iterable)、迭代器(Iterator)與生成器(Generator)的概念,對大家學習或者使用Python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-05-05
python開發(fā)之thread實現(xiàn)布朗運動的方法
這篇文章主要介紹了python開發(fā)之thread實現(xiàn)布朗運動的方法,實例分析了Python基于多線程實現(xiàn)繪圖的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
pytorch sampler對數(shù)據(jù)進行采樣的實現(xiàn)
今天小編就為大家分享一篇pytorch sampler對數(shù)據(jù)進行采樣的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12

