【python教程】对多线程中join()的详细教程

2021/9/24 17:11:22

本文主要是介绍【python教程】对多线程中join()的详细教程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

在 Python 的多线程编程中,经常碰到 thread.join()这样的代码。那么今天咱们用实际代码来解释一下 join 函数的作用。

一、
当一个进程启动之后,会默认产生一个主线程,因为线程是程序执行流的最小单元,当设置多线程时,主线程会创建多个子线程,在python中,默认情况下(其实就是setDaemon(False)),主线程执行完自己的任务以后,就退出了,此时子线程会继续执行自己的任务,直到自己的任务结束,例子见下面。

# 测试多线程中join的功能
import threading, time
def doWaiting():
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(3)
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n"
def doWaiting1():
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(8)
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.start()
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"

print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

代码执行结果如下:

start waiting1: 22:24:20

start join: 22:24:20
start waiting2: 22:24:20


end join: 22:24:20

stop waiting1: 22:24:23

stop waiting2:  22:24:28

二、
当我们使用setDaemon(True)方法,设置子线程为守护线程时,主线程一旦执行结束,则全部线程全部被终止执行,可能出现的情况就是,子线程的任务还没有完全执行结束,就被迫停止,例子见下面。

# 测试多线程中join的功能
'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import threading, time
def doWaiting():
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(3)
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n"
def doWaiting1():
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(8)
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.setDaemon(True)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.setDaemon(True)
thread2.start()
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"

print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

代码执行结果如下:

start waiting1: 22:34:46

start waiting2: 22:34:46
start join: 22:34:46


end join: 22:34:46

三、
没有设置守护线程且没有设置join函数的timeout参数时,主线程将会一直等待,直到子线程全部结束,主线程才结束,程序退出。

代码如下:

# 测试多线程中join的功能
import threading, time
def doWaiting():
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(3)
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n"
def doWaiting1():
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(8)
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.start()
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"
for tt in tsk:
    tt.join()
print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

代码执行结果如下:

start waiting1: 22:41:50

start join: 22:41:50
start waiting2: 22:41:50


stop waiting1: 22:41:53

stop waiting2:  22:41:58

end join: 22:41:58

四、
当没有设置守护线程且join函数的参数timeout=2时,主线程将会等待多个子线程timeout的累加和这样的一段时间,时间一到,主线程结束,但是并没有杀死子线程,子线程依然可以继续执行,直到子线程全部结束,程序退出。

代码如下:

# 测试多线程中join的功能
'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import threading, time
def doWaiting():
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(3)
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n"
def doWaiting1():
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(8)
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.start()
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"
for tt in tsk:
    tt.join(2)
print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

代码执行结果如下:

start waiting1: 23:02:34

start waiting2: 23:02:34
start join: 23:02:34


stop waiting1: 23:02:37

end join: 23:02:38

stop waiting2:  23:02:42

五、
当设置守护线程join函数的参数timeout=2时,主线程将会等待多个子线程timeout的累加和这样的一段时间,时间一到,主线程结束,杀死未执行完的子线程,程序退出。

代码如下:

# 测试多线程中join的功能
'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import threading, time
def doWaiting():
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(3)
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n"
def doWaiting1():
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(8)
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.setDaemon(True)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.setDaemon(True)
thread2.start()
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"
for tt in tsk:
    tt.join(2)
print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

代码执行结果如下:

start waiting1: 23:23:57

start waiting2: 23:23:57
start join: 23:23:57


stop waiting1: 23:24:00

end join: 23:24:01


这篇关于【python教程】对多线程中join()的详细教程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程