线程的join方法
方法一:
from threading import Thread
import time
def task():
print('子线程 start')
time.sleep(2)
print('子线程 end')
t = Thread(target=task)
t.start()
t,join()# 等待子线程运行结束
print('主线程')
方法二:
from threading import Thread
import time
def task(name,n):
print(f'{name} start')
time.sleep(2)
print(f'{name} end')
t1 = Thread(target = task,args=('线程1',1))
t2 = Thread(target = task,args = ('线程2',2))
t3 = Thread(target = task,args=('线程3'.3))
start = time.time()
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
end = time.time()
了解进程的join
from multiprocessing import Process
from threading import Thread
import time
def task():
print('进程 开启')
time.sleep(10)
print('进程 结束')
def task2():
print('子线程 开启')
time.sleep(2)
print('子线程 结束')
if __name__ == '__main__':
p = Process(target=task)
t = Thread(target = task2)
t.start()
p.start()
print('子进程join开始')
p.join
print('主')