pyhton thread lock

Question: have Global Interpreter Lock why do you need a lock?

A: The global interpreter lock is in Cpython interpreter, the same time, multiple threads can only have one thread is scheduled cpu

  It is between threads and cpu lock, time has passed between threads and cpu, even if there GIL, can not guarantee the absolute security of data

Lock Category

1, mutex

2, deadlock

3, recursive lock

# Although global interpreter lock, the data security problem still occurs 
from Threading Import the Thread
 Import Time 


DEF Test ():
     Global n- 
    TEMP = n- 
    the time.sleep ( . 1 ) 
    n- = TEMP -. 1 


n- = 10 
t_li = []
 for I in Range (. 5 ): 
    T = the Thread (target = Test) 
    t_li.append (T) 
    t.start () 
[t.join () for T in t_li]
 Print (n-)     # . 9

Mutex

# 使用锁,解决了数据安全问题
from threading import Thread, Lock
import time


def test(lock):
    lock.acquire()
    global n
    temp = n
    time.sleep(1)
    n = temp - 1
    lock.release()


n = 10
lock = Lock()
t_li = []
for i in range(5):
    t = Thread(target=test, args=(lock, ))
    t_li.append(t)
    t.start()
[t.join() for t in t_li]
print(n)    # 5

Deadlock

# Scientists noodles 
from Threading Import Lock
 from Threading Import the Thread
 Import Time 
Noddle = Lock () 
Chopsticks = Lock () 


DEF test1 (name): 
    noddle.acquire () 
    Print ( ' % S got pasta ' % name)
     # Time. SLEEP (2) 
    chopsticks.acquire ()
     Print ( ' % S get chopsticks ' % name)
     Print ( ' % S noodles ' % name)
    # time.sleep(1)
    chopsticks.release()
    noddle.release()


def test2(name):
    chopsticks.acquire()
    print('%s拿到筷子' % name)
    time.sleep(0.3)
    noddle.acquire()
    print('%s拿到面条' % name)
    print('%s吃面' % name)
    noddle.release()
    chopsticks.release()


t1 = Thread(target=test1, args=('tom', ))
t1.start()
t2 = Thread(target=test2, args=('abc', ))
t2.start()

t3 = Thread(target=test1, args=('joker', ))
t3.start()
t4 = Thread(target=test2, args=('ff', ))
t4.start()

Recursive lock

# Recursive lock, a plurality acquire () does not cause a deadlock 
from Threading Import RLOCK
 from Threading Import the Thread 
A = RLOCK () 


DEF Test (): 
    a.acquire () 
    a.acquire () 
    a.acquire () 
    Print ( ' Hello, World ' ) 


the Thread (target = Test) .start ()

 

# Scientists noodles recursive lock 
from Threading Import RLOCK
 from Threading Import the Thread
 Import Time 
Noddle = Chopsticks = RLOCK () 


DEF test1 (name): 
    noddle.acquire () 
    Print ( ' % S got pasta ' % name)
     # the time.sleep (2) 
    chopsticks.acquire ()
     Print ( ' % S get chopsticks ' % name)
     Print ( ' % S noodles ' % name)
     # the time.sleep (. 1)
    chopsticks.release()
    noddle.release()


def test2(name):
    chopsticks.acquire()
    print('%s拿到筷子' % name)
    time.sleep(0.3)
    noddle.acquire()
    print('%s拿到面条' % name)
    print('%s吃面' % name)
    noddle.release()
    chopsticks.release()


t1 = Thread(target=test1, args=('tom', ))
t1.start()
t2 = Thread(target=test2, args=('abc', ))
t2.start()
t3 = Thread(target=test1, args=('joker', ))
t3.start()
t4 = Thread(target=test2, args=('ff', ))
t4.start()

 

Guess you like

Origin www.cnblogs.com/wt7018/p/11069111.html