The content of this article is about the usage examples (code) of the four locks in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Lock mutex
Before use
num = 0 def a(): global num for _ in range(10000000): num += 1 def b(): global num for _ in range(10000000): num += 1 if __name__ == '__main__': t1=Thread(target=a) t1.start() t2=Thread(target=b) t2.start() t1.join() t2.join() print(num) #基本永远会小于20000000
After use
num = 0 def a(lock): global num for _ in range(1000000): with lock: num += 1 def b(lock): global num for _ in range(1000000): with lock: num += 1 if __name__ == '__main__': lock = threading.Lock() t1=Thread(target=a, args=(lock,)) t1.start() t2=Thread(target=b, args=(lock,)) t2.start() t1.join() t2.join() print(num) #永远会输出20000000
RLock reuse lock
#在之前的代码中永远不可能出现锁在没释放之前重新获得锁,但rlock可以做到,但只能发生在一个线程中,如: num = 0 def a(lock): with lock: print("我是A") b(lock) def b(lock): with lock: print("我是b") if __name__ == '__main__': lock = threading.Lock() t1 = Thread(target=a, args=(lock,)) t1.start() #会发生死锁,因为在第一次还没释放锁后,b就准备上锁,并阻止a释放锁
After use
if __name__ == '__main__': lock = threading.RLock() #只需要改变锁为RLock程序马上恢复 t1 = Thread(target=a, args=(lock,)) t1.start()
Condition synchronization lock
#这个程序我们模拟甲乙对话 Jlist = ["在吗", "干啥呢", "去玩儿不", "好吧"] Ylist = ["在呀", "玩儿手机", "不去"] def J(list): for i in list: print(i) time.sleep(0.1) def Y(list): for i in list: print(i) time.sleep(0.1) if __name__ == '__main__': t1 = Thread(target=J, args=(Jlist,)) t1.start() t1.join() t2 = Thread(target=Y, args=(Ylist,)) t2.start() t2.join() #上面的程序输出后发现效果就是咱们想要的,但是我们每次输出后都要等待0.1秒,也无法正好确定可以拿到时间片的最短时间值,并且不能保证每次正好都是另一个线程执行。因此,我们用以下方式,完美解决这些问题。
After use
Jlist = ["在吗", "干啥呢", "去玩儿不", "好吧"] Ylist = ["在呀", "玩儿手机", "不去","哦"] def J(cond, list): for i in list: with cond: print(i) cond.notify() cond.wait() def Y(cond, list): for i in list: with cond: cond.wait() print(i) cond.notify() if __name__ == '__main__': cond = threading.Condition() t1 = Thread(target=J, args=(cond, Jlist)) t2 = Thread(target=Y, args=(cond, Ylist)) t2.start() t1.start() #一定保证t1启动在t2之后,因为notify发送的信号要被t2接受到,如果t1先启动,会发生阻塞。
Seamplore semaphore
Before use
class B(threading.Thread): def __init__(self, name): super().__init__() self.name = name def run(self): time.sleep(1) print(self.name) class A(threading.Thread): def __init__(self): super().__init__() def run(self): for i in range(100): b = B(i) b.start() if __name__ == '__main__': a = A() a.start() #执行后发现不断在输出
After use
class B(threading.Thread): def __init__(self, name, sem): super().__init__() self.name = name self.sem = sem def run(self): time.sleep(1) print(self.name) sem.release() class A(threading.Thread): def __init__(self, sem): super().__init__() self.sem = sem def run(self): for i in range(100): self.sem.acquire() b = B(i, self.sem) b.start() if __name__ == '__main__': sem = threading.Semaphore(value=3) a = A(sem) a.start() #通过执行上面的代码,我们发现一次只能输出三个数字,sem控制访问并发量
The above is the detailed content of Examples of using four types of locks in Python (code). For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
