启动一个进程后会默认产生一个主线程,设置多线程时,会创建很多的子线程,这里有一个很关键的参数设置,守护进程Daemon,默认情况下,setDaemon(False),这种情况主线程结束后就退出了,如果子线程还没有结束可以继续执行,而当我们开启守护进程时(即setDaemon(True))主线程一结束,无论子线程有没有执行完都统统被杀死,如下:
默认情况下:
import time, threading
def thread_test():
n = 0
while n < 5 :
n = n + 1
print("thread %s is running...: %s" % (threading.current_thread().name,n))
time.sleep(1)
print("thread %s is end." % threading.current_thread().name)
if __name__ == "__main__":
print("thread %s is running..." % threading.current_thread().name)
s1 = time.time()
t = threading.Thread(target=thread_test,name="MyTread")
t.start()
print("thread %s is end." % threading.current_thread().name)
s2 = time.time()
print("timeused : %s" % (s2 - s1))
执行结果:
thread MainThread is running...
thread MyTread is running...: 1
thread MainThread is end.
timeused : 0.0015039443969726562
thread MyTread is running...: 2
thread MyTread is running...: 3
thread MyTread is running...: 4
thread MyTread is running...: 5
thread MyTread is end.
setDaemon(True) (setDarmon要在start前):
import time, threading
def thread_test():
n = 0
while n < 5 :
n = n + 1
print("thread %s is running...: %s" % (threading.current_thread().name,n))
time.sleep(1)
print("thread %s is end." % threading.current_thread().name)
if __name__ == "__main__":
print("thread %s is running..." % threading.current_thread().name)
s1 = time.time()
t = threading.Thread(target=thread_test,name="MyTread")
t.setDaemon(True)
t.start()
print("thread %s is end." % threading.current_thread().name)
s2 = time.time()
print("timeused : %s" % (s2 - s1))
执行结果:
thread MainThread is running...
thread MyTread is running...: 1
thread MainThread is end.
timeused : 0.0020294189453125
而join的功能是完成线程同步,即主线程任务结束后进入阻塞状态,等待子线程任务全部结束后再终止。join有个timeout参数,默认为None,当为守护进程模式时(setDaemon(True)),如果等待了timeout这么多时间子线程无论有没有执行完都全部杀死。不在守护进程模式时(即默认状态),等待了timeout时间后,主线程结束,但是子线程继续执行。
默认情况下使用join:
import time, threading
def thread_test():
n = 0
while n < 5 :
n = n + 1
print("thread %s is running...: %s" % (threading.current_thread().name,n))
time.sleep(1)
print("thread %s is end." % threading.current_thread().name)
if __name__ == "__main__":
print("thread %s is running..." % threading.current_thread().name)
s1 = time.time()
t = threading.Thread(target=thread_test,name="MyTread")
t.start()
t.join()
print("thread %s is end." % threading.current_thread().name)
s2 = time.time()
print("timeused : %s" % (s2 - s1))
执行结果:
thread MainThread is running...
thread MyTread is running...: 1
thread MyTread is running...: 2
thread MyTread is running...: 3
thread MyTread is running...: 4
thread MyTread is running...: 5
thread MyTread is end.
thread MainThread is end.
timeused : 5.005026817321777
setDaemon(True) (setDarmon要在start前):
import time, threading
def thread_test():
n = 0
while n < 5 :
n = n + 1
print("thread %s is running...: %s" % (threading.current_thread().name,n))
time.sleep(1)
print("thread %s is end." % threading.current_thread().name)
if __name__ == "__main__":
print("thread %s is running..." % threading.current_thread().name)
s1 = time.time()
t = threading.Thread(target=thread_test,name="MyTread")
t.setDaemon(True)
t.start()
t.join()
print("thread %s is end." % threading.current_thread().name)
s2 = time.time()
print("timeused : %s" % (s2 - s1))
执行结果:
thread MainThread is running...
thread MyTread is running...: 1
thread MyTread is running...: 2
thread MyTread is running...: 3
thread MyTread is running...: 4
thread MyTread is running...: 5
thread MyTread is end.
thread MainThread is end.
timeused : 5.003417491912842
我讲的不够详细,我的笔记来源于这位大佬的博客:https://www.cnblogs.com/cnkai/p/7504980.html
`````````````````````````````````````````````````````发现自己做错了一件事情```````````````````````````````````````````````````````````````````
我本来是想创建多个线程,创建线程的方法有俩:
1.使用Thread类创建:
from threading import Thread
t = Thread(target=function_name,args=(function_paramter,),name=thread_name)
2.使用继承类创建
from threading import Thread
# 创建一个类,必须继承Thread
class MyThread(Thread):
def __init__(self, parameter):
# 需要执行父类的初始化方法
Thread.__init__(self)
# 如果有参数,可以封装在类里面
self.parameter = parameter
def function_name(self, parameter):
#具体逻辑
pass
def run(self):
# 线程从此处开始运行
self.function_name(self.parameter)
# 如果有参数,实例化的时候需要把参数传递过去
t = MyThread(parameter)
# 使用start()来启动线程
t.start()
所以我上面举的例子中我只创建了一个线程,即一个主线程+一个子线程,如果我们想创建多个线程则需要把每个线程放置数组中:
thread_list = []
for i in range(1,11):
t = MyThread(parameter)
thread_list.append(t)
t.start()
# 在这里统一执行线程等待的方法
for t in thread_list:
t.join()
又发现了一篇好博客:https://www.imooc.com/article/16198