这篇文章主要介绍了python多线程及多线程有序性怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python多线程及多线程有序性怎么实现文章都会有所收获,下面我们一起来看看吧。多线程一般用于同时调用多个函数,cpu时间片轮流分配给多个任务。 优点是提高cpu的使用率,使计算机减少处理多个任务的总时间;缺点是如果有全局变量,调用多个函数会使全局变量被多个函数免费云主机域名修改,造成计算错误,这使需要使用join方法或者设置局部变量来解决问题。python使用threading模块来实现多线程,threading.join()方法是保证调用join的子线程完成后,才会分配cpu给其他的子线程,从而保证线程运行的有序性。我们首先创建三个实例,t1,t2,t3 t1实例调用function1函数,t2和t3函数调用function11函数,他们都是对全局变量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不变。代码如下:结果如下: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关于“python多线程及多线程有序性怎么实现”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“python多线程及多线程有序性怎么实现”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注百云主机行业资讯频道。
相关推荐: vite electron-builder打包配置的方法是什么
这篇文章主要介绍“vite electron-builder打包配置的方法是什么”,在日常操作中,相信很多人在vite electron-builder打包配置的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vite …
免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。