python-线程三(锁)

Posted su_sir

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-线程三(锁)相关的知识,希望对你有一定的参考价值。

锁:可以保证线程同步和线程安全

  1)Lock锁

方式1:Lock
from threading import Thread, Lock
import threading


def run(lock, num): 
    lock.acquire()  # 获得锁
    threadName = threading.current_thread().getName()  # 取得当前线程的线程名
    print("%s, Hello Num: %s" %(threadName, num))
    lock.release()  # 释放锁


if __name__ == __main__: 
    lock = Lock()  # 创建一个共享锁实例
    for num in range(20):
        Thread(name=Thread-%s % str(num), target=run, args=(lock, num)).start()


# 方式2
import threading
import time
data = 0
lock = threading.Lock()  # 创建一个锁对象


def func():
    global data
    print("%s acquire lock...
" % threading.currentThread().getName())
    if lock.acquire():
        print("%s get lock...
" % threading.currentThread().getName())
        data += 1  # must lock
        time.sleep(2)  # 其它操作
        print("%s release lock...
" % threading.currentThread().getName())  # 调用release()将释放锁
        lock.release()


startTime = time.time()
t1 = threading.Thread(target=func)
t2 = threading.Thread(target=func)
t3 = threading.Thread(target=func)
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
endTime = time.time()
print("used time is", endTime - startTime)

  2)Semaphore锁

from threading import Thread, Lock
import threading
import time


def worker(s, i):
    s.acquire()
    print(threading.current_thread().name + " acquire")
    time.sleep(i)
    print(threading.current_thread().name + " release")
    s.release()

    
if __name__ == "__main__":
    # 设置限制最多3个线程同时访问共享资源
    s = threading.Semaphore(3)
    for i in range(5):
        t = Thread(target=worker, args=(s, i * 2))
        t.start()

 

死锁

import threading 
import time
lock1 = threading.Lock() 
lock2 = threading.Lock() 
print(lock1, lock2)


class T1(threading.Thread): 
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.t_name = name
        
    def run(self):
        lock1.acquire()
        time.sleep(1)   # 睡眠的目的是让线程2获得调度,得到第二把锁
        print(in thread T1,self.t_name)
        time.sleep(2)
        lock2.acquire()  # 线程1请求第二把锁
        print(in lock l2 of T1)
        lock2.release()
        lock1.release()


class T2(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.t_name = name
        
    def run(self):
        lock2.acquire()
        time.sleep(2)  # 睡眠的目的是让线程1获得调度,得到第一把锁
        print(in thread T2,self.t_name)
        lock1.acquire()  # 线程2请求第一把锁
        print(in lock l1 of T2)
        lock1.release()
        lock2.release()
        
        
def test():
    thread1 = T1(A)
    thread2 = T2(B)
    thread1.start()
    thread2.start()
    
    
if __name__== __main__:
    test()

# 伪代码:

死锁:
锁1
锁2


线程1:
锁1.acquire() # 获得锁1
sleep(2)
锁2.acquire() # 获取锁2 的时候要等线程2释放了才可以获取
锁1.release() # 要等上一步执行了才可以释放锁1


线程2:
锁2.acquire() # 获得锁2
sleep(2)
锁1.acquire() # 获取锁1 的时候要等线程1释放了才可以获取
锁2.release() 要等上一步执行了才可以释放锁2

 

以上是关于python-线程三(锁)的主要内容,如果未能解决你的问题,请参考以下文章

[Python3] 043 多线程 简介

JUC并发编程 共享模式之工具 JUC CountdownLatch(倒计时锁) -- CountdownLatch应用(等待多个线程准备完毕( 可以覆盖上次的打印内)等待多个远程调用结束)(代码片段

为啥基于锁的程序不能组成正确的线程安全片段?

python多线程

Python 线程互斥锁 Lock

并发技术12线程锁技术的使用