温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中怎么使用队列Queue来改造转账场景

发布时间:2022-02-24 16:45:01 来源:亿速云 阅读:213 作者:iii 栏目:开发技术

今天小编给大家分享一下Python中怎么使用队列Queue来改造转账场景的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

一、看看转账场景的问题

前面有两篇文章展示了转账反复读写amount,导致结果出错。

xuewei_account = dict() xuewei_account['amount'] = 100 # amount为负数即是转出金额 def transfer(money):     for i in range(100000):         xuewei_account['amount'] = xuewei_account['amount'] + money

我们前几篇使用多个线程反复转长:+1和-1。

按常理,结果应该仍旧是100.

这个是全部代码:

import random import threading import datetime import time xuewei_account = dict() xuewei_account['amount'] = 100 # amount为负数即是转出金额 def transfer(money):     for i in range(100000):         xuewei_account['amount'] = xuewei_account['amount'] + money # 创建20个任务重复给学委账户转账 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("活跃线程数:", threading.active_count()) print("活跃线程:", threading.current_thread().name) print("学委账户余额:", xuewei_account)

等待所有转账线程运行结束,我们看到结果是错误的:

Python中怎么使用队列Queue来改造转账场景

二、这种问题怎么使用队列来解决呢?

前面说了,多线程反复读写共享数据,是问题的根源。

改代码为同步互斥模式,保证任意一个时间一个线程更新共享数据,那么问题就解决了。(这前面也展示了,用的是Lock锁的方案)

这个能怎么用队列呢?

可以先思考10秒,根据学习到的加锁和队列的特性,想想这个怎么做。

好,答案现在揭晓:

Queue这个队列有多个函数,一个是put函数,一个是get函数。

一个负责放入数据到队尾,一个可以从对头取出元素。

刚好适合转账业务,我们是不是可以把每次转账操作变成一个一个指令/事件。 比如下面的:

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个线程,每个10万次数据读写,共200万个事件。

所以我们可以把这个事情转换为:200万个转账事件。

因为Queue是线程安全的,所以我们可以并发200万次转账,另外交给一线程进行转账处理。

这样就保证每次只有一个线程对xuewei_account学委账户进行读写。

改造,使用队列来解决问题

展示代码:

import random import threading import datetime import time import queue q = queue.Queue() xuewei_account = dict() xuewei_account['amount'] = 100 # amount为负数即是转出金额 def transfer(money):     for i in range(100000):         q.put(money) def handle_amount():     while not q.empty():         amount = q.get()         xuewei_account['amount'] += amount def monitor_q():     counter = 0     time.sleep(3)     while counter < 1000 and not q.empty():         print("q size:", q.qsize())         time.sleep(3)         counter+=1 q_thread = threading.Thread(name="Q监控", target=monitor_q) q_thread.start() # 创建20个任务重复给学委账户转账 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="处理转账专线", target=handle_amount) vip_thread.start() for t in threads:     t.join() vip_thread.join() print("-" * 16) print("活跃线程数:", threading.active_count()) print("活跃线程:", threading.current_thread().name) print("学委账户余额:", xuewei_account)

这里运行了多个线程执行转账(发送转账金额进队列)。

然后运行一个vip通道(单独线程)处理学委账户的转账业务。

同时也运行了一个监控队列的线程,每隔一段时间打印队列的任务情况。

下面是运行结果,运行几次结果都是正确的。

Python中怎么使用队列Queue来改造转账场景

运行几次最终账户余额都是100, 改造成功。

以上就是“Python中怎么使用队列Queue来改造转账场景”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI