当前位置:网站首页>Notes on concurrent programming of vegetables (V) thread safety and lock solution
Notes on concurrent programming of vegetables (V) thread safety and lock solution
2022-04-23 10:34:00 【Ape knowledge】
Series index : Concurrent programming notes of vegetables | Python Concurrent programming details ( Continuous updating ~)
One 、 Introduction to thread safety concepts
Two 、Lock Used to solve thread safety problems
The first way is to manually obtain , Manually release... After execution , It cannot be locked without releasing other programs . The second is easier to use ,with
It will release resources after program execution .
3、 ... and 、 Code details
We use threading Module built in Lock
Method to create a lock object , Take withdrawing money as an example , The following code can only be executed after obtaining the lock .
stay main In the function, we initialize a Account object , Create two threads ta and tb, Let both threads start executing , The first thread executes normally , The second thread can only obtain the lock after the first thread is executed . If this code is unlocked , If two threads execute at the same time, the balance will be negative .
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," Take money successfully ")
account.balance -= amount
print(threading.current_thread().name," balance ", account.balance)
else:
print(threading.current_thread().name," Failed to withdraw money , Lack of balance ")
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()
utilize Lock To solve thread safety problems is very common , In the next article, let's use Thread pool
To solve this kind of problem , Semaphore mechanism
It also plays a big role in thread safety , Welcome to your attention , I will keep updating ~
Python Advanced concurrent programming is continuously updated , welcome
Like collection
+Focus on
Last one : Concurrent programming notes of vegetables |( Four )Python Actual producer - Consumer mode multithreaded crawler
Next : Concurrent programming notes of vegetables |( 6、 ... and ) How to use thread pool to transform crawler program
My level is limited , Please comment and correct the deficiencies in the article in the comment area below ~If feelings help you , Point a praise Give me a hand ~
Share... From time to time Interesting 、 Have a material 、 Nutritious content , welcome Subscribe to follow My blog , Looking forward to meeting you here ~
版权声明
本文为[Ape knowledge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230619310059.html
边栏推荐
- DBA common SQL statements (2) - SGA and PGA
- Operation of 2022 tea artist (primary) test question simulation test platform
- 59. Spiral matrix (array)
- 部署jar包
- Sim Api User Guide(8)
- Sim Api User Guide(5)
- Turn: Maugham: reading is a portable refuge
- Jerry's more accurate determination of abnormal address [chapter]
- 得到知识服务app原型设计比较与实践
- Common SQL statements of DBA (6) - daily management
猜你喜欢
Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file
得到知识服务app原型设计比较与实践
Charles function introduction and use tutorial
Shell script interaction free
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
Solution architect's small bag - 5 types of architecture diagrams
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
Operation of 2022 tea artist (primary) test question simulation test platform
【leetcode】199.二叉树的右视图
随机推荐
lnmp的配置
微信小程序中app.js文件、组件、api
Shell script interaction free
/Can etc / shadow be cracked?
What are Jerry's usual program exceptions? [chapter]
Realizing data value through streaming data integration (5) - stream processing
Realize data value through streaming data integration (3) - real-time continuous data collection
209、长度最小的子数组(数组)
SSH利用私钥无密钥连接服务器踩坑实录
Solve the problem of installing VMware after uninstalling
MySql常用语句
Problems of class in C # and database connection
SQLServer 查询数据库死锁
转:毛姆:阅读是一座随身携带的避难所
C language - custom type
Leetcode22:括号生成
Download and installation steps of xshell + xftp
349. Intersection of two arrays
Operation of 2022 tea artist (primary) test question simulation test platform
1. Sum of two numbers (hash table)