文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

多线程python的实现及多线程有序性

2024-04-02 19:55

关注

前言

多线程一般用于同时调用多个函数,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的实现及多线程有序性的文章就介绍到这了,更多相关python多线程内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯