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/01 Report--
Today, Xiaobian will share with you how to use Queue to transform the relevant knowledge points of transfer scenarios in Python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you will gain something after reading this article. Let's learn about it together.
I. Look at the problem of transfer scenario
The previous two articles showed that the transfer repeatedly read and write amount, resulting in an error in the result.
xuewei_account = dict()xuewei_account ['amount '] = 100# amount is negative, that is, the amount transferred def transfer(money): for i in range(100000): xuewei_account['amount'] = xuewei_account['amount'] + money
Our previous articles used multiple threads to iterate: +1 and-1.
Logically, it should still be 100.
This is the full code:
import randomimport threadingimport datetimeimport timexuewei_account = dict()xuewei_account <$'amount '] = 100# amount is negative is the amount transferred def transfer(money): for i in range(100000): xuewei_account <$'amount '] = xuewei_account <$'amount'] + money#Create 20 tasks repeatedly to transfer threads = []for i in range(10): t1 = threading.Thread(target=lambda: transfer(-1)) threads.append(t1) t2 = threading.Thread(target=lambda: transfer(1)) threads.append(t2)for t in threads: t.start()for t in threads: t.join()print("-" * 16)print("active threads:", threading.active_count())print("active threads:", threading.current_thread().name)print("balance of school committee account: ", xuewei_account)
Waiting for all transfer threads to finish running, we see that the result is wrong:
2. How can this problem be solved by using queues?
As mentioned above, multi-thread repeated reading and writing of shared data is the root cause of the problem.
Change the code to synchronous mutex mode, ensure that a thread updates shared data at any time, then the problem is solved. (This is also shown earlier, using the Lock scheme)
How can I use this queue?
Think about it for 10 seconds and think about how to do it based on the characteristics of the lock and queue you have learned.
Okay, here's the answer:
Queue This queue has multiple functions, one is the put function and one is the get function.
One is responsible for putting data to the end of the queue, and one can take elements from the head.
Just suitable for money transfer business, can we turn each transfer operation into a command/event? For example:
event(amount=1,acount=xuewei_account).... event(amount=-1,acount=xuewei_account).... event(amount=1,acount=xuewei_account).... event(amount=-1,acount=xuewei_account)
20 threads, 100,000 data reads and writes each, 2 million events in total.
So we can translate this into two million transfers.
Because Queue is thread-safe, we can make 2 million transfers concurrently and hand over another thread for transfer processing.
This ensures that only one thread reads and writes to the xuewei_account account at a time.
Transform, use queues to solve problems
Display Code:
import randomimport threadingimport datetimeimport queue = queue.Queue()xuewei_account = dict() xuewei_account <$'amount '] = 100# amount is negative, that is, the amount transferred def transfer(money): for i in range(100000): q.put(money)def handle_amount(): while not q.empty(): amount = q.get() xuewei_account['amount'] += amountdef monitor_q(): counter = 0 time.sleep(3) while counter < 1000 and not q.empty(): print("q size:", q.qsize()) time.sleep(3) counter+=1q_thread = threading.Thread(name="Q monitor", target=monitor_q)q_thread.start()#Create 20 tasks repeat transfer threads = []for i in range(10): t1 = threading.Thread(target=lambda: transfer(-1)) threads.append(t1) t2 = threading.Thread(target=lambda: transfer(1)) threads.append(t2)for t in threads: t.start()vip_thread = threading.Thread(name="Process Transfer Line", target=handle_amount)vip_thread.start()for t in threads: t.join()vip_thread.join()print("-" * 16)print("active threads:", threading.active_count())print("active threads:", threading.current_thread().name)print("school committee account balance: ", xuewei_account)
There are multiple threads running to execute the transfer (sending the transfer amount into the queue).
Then run a vip channel (separate thread) to handle the transfer business of the school committee account.
It also runs a thread that monitors the queue and prints the queue's tasks at regular intervals.
Here are the results of the run, and the results are correct several times.
After several runs, the final account balance is 100, and the transformation is successful.
The above is all the content of this article "How to use Queue to transform transfer scene in Python". Thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to 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.