python多线程有序性如何理解,无序问题问题如何解决

Admin 2022-08-04 群英技术资讯 304 次浏览

这篇文章主要介绍了python多线程有序性如何理解,无序问题问题如何解决相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python多线程有序性如何理解,无序问题问题如何解决文章都会有所收获,下面我们一起来看看吧。


前言

多线程一般用于同时调用多个函数,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()的方法。

二、“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()方法可以解决多线程无序问题

三、threading.Thread()的常用参数

1.group:默认值None,为了实现ThreadGroup类而保留
2.target:在start方法中调用的可调用对象,即需要开启线程的可调用对象,比如函数、方法
3.name:默认为“Thread-N”,字符串形式的线程名称
4.args:默认为空元组,参数target中传入的可调用对象的参数元组
5.kwargs:默认为空字典{},参数target中传入的可调用对象的关键字参数字典
6.daemon:默认为None

总结


“python多线程有序性如何理解,无序问题问题如何解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业技术相关的知识可以关注群英网络网站,小编每天都会为大家更新不同的知识。 群英智防CDN,智能加速解决方案

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

猜你喜欢

成为群英会员,开启智能安全云计算之旅

立即注册
专业资深工程师驻守
7X24小时快速响应
一站式无忧技术支持
免费备案服务
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
在线客服
微信公众号
返回顶部
返回顶部 返回顶部
在线客服
在线客服