1.使用threading.Thread創建
import threading
from time import sleep
"""
使用threading.Thread創建
"""
def task1():
for i in range(5):
print("任務1正在執行~~~")
sleep(1)
def task2():
for i in range(5):
print("任務2正在執行~~~")
sleep(1)
if __name__ == '__main__':
t1 = threading.Thread(target=task1)
t2 = threading.Thread(target=task2)
t1.start()
t1.join() # 先讓task1執行
t2.start()
"""
輸出結果:
任務1正在執行~~~
任務1正在執行~~~
任務1正在執行~~~
任務1正在執行~~~
任務1正在執行~~~
任務2正在執行~~~
任務2正在執行~~~
任務2正在執行~~~
任務2正在執行~~~
任務2正在執行~~~
"""