当前位置:网站首页>菜菜的并发编程笔记 |(五)线程安全问题以及Lock解决方案
菜菜的并发编程笔记 |(五)线程安全问题以及Lock解决方案
2022-04-23 06:19:00 【猿知】

系列索引:菜菜的并发编程笔记 | Python并发编程详解(持续更新~)
一、线程安全概念介绍

二、Lock用于解决线程安全问题

第一种方式先手动获取,在执行完后手动释放,若不释放其他程序无法上锁。第二种使用起来更加简单,with会在程序执行结束后自定释放资源。
三、代码详解
我们使用threading模块内置的Lock方法创建一个lock对象,这里以取钱为例,只有在获取到锁以后才可以执行下面的代码。
在main函数中我们初始化一个Account对象,创建两个线程ta和tb,让两个线程都开始执行,第一个线程正常执行,第二个线程只有等第一个线程执行完后才能获得锁。如果这段代码不加锁,两个线程同时执行就会造成余额为负数。
import threading
import time
lock = threading.Lock()
class Account:
def __init__(self, balance):
self.balance = balance
def draw(account, amount):
with lock:
if account.balance >= amount:
time.sleep(0.1)
print(threading.current_thread().name,"取钱成功")
account.balance -= amount
print(threading.current_thread().name,"余额", account.balance)
else:
print(threading.current_thread().name,"取钱失败,余额不足")
if __name__ == "__main__":
account = Account(1000)
ta = threading.Thread(name="ta", target=draw, args=(account, 800))
tb = threading.Thread(name="tb", target=draw, args=(account, 800))
ta.start()
tb.start()
利用Lock来解决线程安全问题十分常见,下一篇文章我们来讲使用线程池来解决该类问题,信号量机制在线程安全中也大有作用,欢迎关注,我会持续更新~
Python进阶之并发编程篇持续更新,欢迎
点赞收藏+关注
上一篇:菜菜的并发编程笔记 |(四)Python实战生产者-消费者模式多线程爬虫
下一篇:菜菜的并发编程笔记 |(六)如何使用线程池改造爬虫程序详解
本人水平有限,文章中不足之处欢迎下方评论区批评指正~如果感觉对你有帮助,点个赞 支持一下吧 ~
不定期分享 有趣、有料、有营养内容,欢迎 订阅关注 我的博客 ,期待在这与你相遇 ~
版权声明
本文为[猿知]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Magic_Zsir/article/details/122672798
边栏推荐
- PyTorch 12. Hook usage
- Intuitive understanding of torch nn. Unfold
- 北峰油气田自组网无线通信对讲系统解决方案
- 南方投资大厦SDC智能通信巡更管理系统
- 字节数仓实习生面试sql题
- 学习笔记7-深度神经网络优化
- Statement of American photography technology suing Tianmu media for using volcanic engine infringement code
- 基于可视化结构的身份证号码校验系统-树莓派实现
- presto日期函数的使用
- 免费开源农业物联网云平台(Version:3.0.1)
猜你喜欢

vim+ctags+cscpope开发环境搭建指南

DMR system solution of Kaiyuan MINGTING hotel of Fengqiao University

重大安保事件应急通信系统解决方案

How to improve the service efficiency of the hotel without blind spots and long endurance | public and Private Integrated walkie talkie?

北峰通信助力湛江市消防支队构建PDT无线通信系统

基于可视化结构的身份证号码校验系统-树莓派实现

Lead the industry trend with intelligent production! American camera intelligent video production platform unveiled at 2021 world Ultra HD Video Industry Development Conference

可视化常见问题解决方案(八)数学公式

PC端一次启动多个微信

Tensorflow安装后ImportError: DLL load failed: 找不到指定的模块,且国内安装缓慢
随机推荐
Machine vision series (02) -- tensorflow2 3 + win10 + GPU installation
商业版阿里MQ普通消息发送订阅Demo
Beifeng communication helps Zhanjiang fire brigade build PDT wireless communication system
Intelligent communication solution of Hainan Phoenix Airport
安装tui-editor失败,快速解决方案
Meishe technology launches professional video editing solution for desktop -- Meiying PC version
大型体育赛事无线通信系统
Pycharm
Flexible blind patch of ad hoc network | Beifeng oil and gas field survey solution
Urban emergency management - urban emergency communication command and dispatching system
The difference between null and undefined
unhandled system error, NCCL version 2.7.8
quill-editor图片缩放、在一个页面使用多个富文本框、quill-editor上传图片地址为服务器地址
基于可视化结构的身份证号码校验系统-树莓派实现
hql求一个范围内最大值
学习资料
城市应急管理|城市突发事故应急通信指挥调度系统
开发板如何ping通百度
golang实现MD5,SHA256,bcrypt加密
可视化之路(十一)matplotlib颜色详解