06 併發程式設計 執行緒和鎖

2022-05-18 15:02:50 字數 3956 閱讀 7701

什麼是執行緒

程序:資源分配單位

執行緒:cpu執行單位(實體),每乙個py檔案中就是乙個程序,乙個程序中至少有乙個執行緒

執行緒的兩種建立方式:

from multiprocessing import

process

deff1(n):

print(n,'

號執行緒')if

__name__ == '

__main__':

t1 = thread(target=f1,args=(1,))

t1.start()

print('

主線程')

from threading import

thread

class

mythread(thread):

def__init__

(self,name):

super().

__init__

() self.name =name

defrun(self):

print('

hello:

' +self.name)

if__name__ == '

__main__':

t = mythread('

haha')

t.start()

print('

主線程結束

')

執行緒的建立和銷毀,相對於程序來說開銷特別小

執行緒之間資源共享,共享的是同乙個程序中的資源,  資源共享就涉及到資料安全問題,加鎖來解決

執行緒鎖

from threading import thread,lock

def f1: 

loc.acquire()

**loc.release()

main

loc = lock()

t = thread(target=f1,args=(loc,)

from multiprocessing import

queue

import

queue

import

time

from threading import

lock,thread

num = 100

deff1(loc):

loc.acquire()

global

num tmp =num

tmp -= 1time.sleep(0.001)

num =tmp

loc.release()

if__name__ == '

__main__':

t_loc =lock()

t_list =

for i in range(10):

t = thread(target=f1,args=(t_loc,))

t.start()

[tt.join()

for tt in

t_list]

print('

主線的num

',num)

鎖:犧牲了效率,保證了資料安

死鎖現象(天長地久,永不分離):

出現在鎖巢狀的時候,雙方互相搶對方已經拿到的鎖,導致雙方互相等待,這就是死鎖現象

import

time

from threading import

thread,lock,rlock

deff1(loca,locb):

loca.acquire()

print('

f1>>1號搶到了a鎖')

time.sleep(1)

locb.acquire()

print('

f1>>1號搶到了b鎖')

locb.release()

loca.release()

deff2(loca,locb):

locb.acquire()

print('

f2>>2號搶到了b鎖')

loca.acquire()

time.sleep(1)

print('

f2>>2號搶到了a鎖')

loca.release()

locb.release()

if__name__ == '

__main__':

loca =lock()

locb =lock()

t1 = thread(target=f1,args=(loca,locb))

t2 = thread(target=f2,args=(loca,locb))

t1.start()

t2.start()

遞迴鎖:解決死鎖現象

rlock  首先本身就是個互斥鎖,維護了乙個計數器,每次acquire就+1,release就-1,當計數器為0的時候,大家才能搶這個鎖

import

time

from threading import

thread, lock, rlock

deff1(loca, locb):

loca.acquire()

print('

f1>>1號搶到了a鎖')

time.sleep(1)

locb.acquire()

print('

f1>>1號搶到了b鎖')

locb.release()

loca.release()

deff2(loca, locb):

locb.acquire()

print('

f2>>2號搶到了b鎖')

loca.acquire()

time.sleep(1)

print('

f2>>2號搶到了a鎖')

loca.release()

locb.release()

if__name__ == '

__main__':

loca = locb =rlock()

t1 = thread(target=f1, args=(loca, locb))

t2 = thread(target=f2, args=(loca, locb))

t1.start()

t2.start()

守護執行緒:守護執行緒:等待所有非守護執行緒的結束才結束

守護程序:主程序**執行結束,守護程序就隨之結束

import time

from threading import thread

from multiprocessing import process

def f1():

time.sleep(2)

print('1號執行緒')

def f2():

time.sleep(3)

print('2號執行緒')

if __name__ == '__main__':

t1 = thread(target=f1,)

t2 = thread(target=f2,)

t2.daemon = true

t1.start()

t2.start()

print('主線程結束')

gil鎖 :cpython直譯器上的一把互斥鎖,當執行緒需要進入cpu做運算時必須乙個乙個經過gil鎖

執行緒的事件,訊號量 與程序的事件,訊號量 用法相同.

Python併發程式設計 執行緒鎖

多執行緒中雖然有gil,但是還是有可能產生資料不安全,故還需加鎖 from threading import lock,thread 互斥鎖 import time def eat1 lock global n lock.acquire temp n time.sleep 0.2 n temp 1 ...

併發程式設計整理筆記06 讀寫鎖

public class readwritelocktest string.valueof i start for int i 1 i 5 i string.valueof i start class mycache public void get string key class mycachel...

併發程式設計之執行緒的讀寫鎖

1.概念 2.讀寫鎖的狀態 3.讀寫鎖特性 4.讀寫鎖的適用場景 5.主要資料型別和應用函式 6.編碼舉例 讀寫鎖實際上是一種特殊的自旋鎖,它把共享資源的訪問劃分成讀者和寫者,讀者只能擁有對共享資源的讀許可權,寫者則需要對共享資源進行寫操作。讀寫鎖並不是兩把鎖,它是乙個名字叫做讀寫鎖的鎖,可以擁有讀...