标签:pre main thread threading span col reading == def
写法一
import time
from threading import Thread
def func(name):
print(f"{name}开始")
time.sleep(0.5)
print(f"{name}结束")
if __name__ == ‘__main__‘:
t1 = Thread(target=func, args=("线程1",))
t2 = Thread(target=func, args=("线程2",))
t1.start()
t2.start()
print("主线程结束")
执行结果:
线程1开始
线程2开始
主线程结束
线程1结束
线程2结束
写法二
import time
from threading import Thread
def func(name):
print(f"{name}开始")
time.sleep(0.5)
print(f"{name}结束")
if __name__ == ‘__main__‘:
t1 = Thread(target=func, args=("线程1",))
t2 = Thread(target=func, args=("线程2",))
t1.start()
t2.start()
print("主线程结束")
执行结果:
线程1开始
线程2开始
主线程结束
线程2结束
线程1结束
两种写法效果是一样的。
标签:pre main thread threading span col reading == def
原文地址:https://www.cnblogs.com/lilyxiaoyy/p/12054441.html