python多线程有序性如何理解,无序问题问题如何解决
Admin 2022-08-04 群英技术资讯 304 次浏览
多线程一般用于同时调用多个函数,cpu时间片轮流分配给多个任务。 优点是提高cpu的使用率,使计算机减少处理多个任务的总时间;缺点是如果有全局变量,调用多个函数会使全局变量被多个函数修改,造成计算错误,这使需要使用join方法或者设置局部变量来解决问题。python使用threading模块来实现多线程,threading.join()方法是保证调用join的子线程完成后,才会分配cpu给其他的子线程,从而保证线程运行的有序性。
我们首先创建三个实例,t1,t2,t3 t1实例调用function1函数,t2和t3函数调用function11函数,他们都是对全局变量l1进行操作
代码如下:
import threading,time l1 = [] #创建RLock锁,acquire几次,release几次 lock = threading.RLock() def function1(x,y): for i in range(x): l1.append(i) if i == 0: time.sleep(1) end_time = time.time() print("t{} is finished in {}s".format(y,end_time -time1 )) def function11(x,y): for i in range(x): l1.append(i) end_time = time.time() print("t{} is finished in {}s".format(y, end_time -time1)) #2.创建子线程:thread类 if __name__ == '__main__': t1 = threading.Thread(target= function1, args = (100,1)) t2 = threading.Thread(target= function11, args = (100,2)) t3 = threading.Thread(target= function11, args = (100,3)) time1 = time.time() print("time starts in {}".format(time1)) t1.start() t2.start() t3.start() print(l1)
结果如下:
runfile('E:/桌面/temp.py', wdir='E:/桌面')
time starts in 1656474963.9487
t2 is finished in 0.0s
t3 is finished in 0.0s
[0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
t1 is finished in 1.0152690410614014s
我们可以看到,全局变量中开头有两个0,而不是按着0,1,2,3的方式按序填充,所以可以得知全局变量在多线程中是被多个函数无序调用的。为了保证多线程有序调用全局变量,我们可以利用threading.join()的方法。
我们重写了function1函数,并命名为function2,t1调用function2函数。t2,t3不变。
代码如下:
import threading,time l1 = [] #创建RLock锁,acquire几次,release几次 lock = threading.RLock() def function1(x,y): for i in range(x): l1.append(i) if i == 0: time.sleep(1) end_time = time.time() print("t{} is finished in {}s".format(y,end_time -time1)) def function11(x,y): for i in range(x): l1.append(i) end_time = time.time() print("t{} is finished in {}s".format(y,end_time -time1)) def function2(x,y): for i in range(x): l1.append(i) if i == 0: time.sleep(1) end_time = time.time() print("t{} is finished in {}s".format(y,end_time -time1)) #2.创建子线程:thread类 if __name__ == '__main__': t1 = threading.Thread(target= function2, args = (100,1)) t2 = threading.Thread(target= function11, args = (100,2)) t3 = threading.Thread(target= function11, args = (100,3)) time1 = time.time() print("time starts in {}".format(time1)) t1.start() t1.join() t2.start() t3.start() print(l1)
结果如下:
runfile('E:/桌面/temp.py', wdir='E:/桌面')
time starts in 1656476057.441827
t1 is finished in 1.0155227184295654s
t2 is finished in 1.0155227184295654s
t3 is finished in 1.0155227184295654s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
由此可见,threading.join()方法可以解决多线程无序问题
1.group:默认值None,为了实现ThreadGroup类而保留
2.target:在start方法中调用的可调用对象,即需要开启线程的可调用对象,比如函数、方法
3.name:默认为“Thread-N”,字符串形式的线程名称
4.args:默认为空元组,参数target中传入的可调用对象的参数元组
5.kwargs:默认为空字典{},参数target中传入的可调用对象的关键字参数字典
6.daemon:默认为None
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
数据清洗是整个数据分析过程的第一步,就像做一道菜之前需要先择菜洗菜一样。 数据分析师经常需要花费大量的时间来清洗数据或者转换格式,这个工作甚至会占整个数据分析流程的80%左右的时间。今天,就来学习一下Python的数据清洗吧!
这篇文章主要为大家介绍了python根据照片获取地理位置及泄露防御,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
在本篇内容里小编给大家总结的是一篇关于python中Task封装协程的知识点总结内容,有兴趣的朋友们可以跟着学习下。
这篇文章主要为大家详细介绍了Python实现简单扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了Python数据分析之 Pandas Dataframe合并和去重操作,文章基于python的相关资料展开详细的内容介绍,需要的小伙伴可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008