In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the situation of python thread safety, the article is very detailed, has a certain reference value, interested friends must read it!
1. Visibility problems, visibility problems caused by CPU cache, so that the operation of a pair of shared variable A can not be immediately seen by thread 2, resulting in thread 2 getting the old value.
2. Atomic problem.
For example, the statement self._balance=self._balance-draw_amount is not an atomic CPU execution command. There are three CPU commands to execute this statement.
Example
Import threadingclass Account: # define constructor def _ _ init__ (self, account_no, balance): ": param account_no: account: param balance: balance" self.account_no = account_no self._balance = balance def draw (self Draw_amount): ": param draw_amount: money to withdraw: return:"if self._balance > draw_amount: print (threading.current_thread (). GetName () + 'successful withdrawal from' + self.account_no +" The amount withdrew from the account is: "+ str (draw_amount) +"\ n ") self._balance = self._balance-draw_amount print ('account balance is' Self._balance) else: print (threading.current_thread (). GetName () + 'from' + self.account_no + "failed to withdraw money\ n") # two threads simultaneously send and withdraw money account = Account ('account one', 2000) threading.Thread (target=account.draw, name=' thread 1', args= (800,)). Start () threading.Thread (target=account.draw, name=' thread 2', args= (800,)). Start () threading.Thread (target=account.draw) Name=' thread 3', args= (800,). Start () these are all the contents of the article "what's wrong with Python thread safety?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.