线程 Threading

import time
from threading import Thread
def func():
while True:
time.sleep(0.5)
print(123)
def func2():
print("func2 start ")
time.sleep(3)
print("func2 end")
t1 = Thread(target=func)
t2 = Thread(target=func2)
t1.setDaemon(True) #将t1变成守护线程
t1.start()
t2.start()
print("主线程代码结束")

输出

func2 start

主线程代码结束

123

123

123

123

123

func2 end

猜你喜欢

转载自www.cnblogs.com/yuncong/p/9696535.html