python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python多线程

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

作者:Efred.D

这篇文章主要介绍了多线程python的实现及多线程有序性,多线程一般用于同时调用多个函数,cpu时间片轮流分配给多个任务

前言

多线程一般用于同时调用多个函数,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多线程内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文