python創建線程示例
來源:懂視網
責編:小采
時間:2020-11-27 14:30:26
python創建線程示例
python創建線程示例: 代碼如下:import threadingfrom time import sleep def test_func(id): for i in range(0,5): sleep(1) print('thread %d is running %d' % (id,i)) threads = []for i in range(0,3): t = threa
導讀python創建線程示例: 代碼如下:import threadingfrom time import sleep def test_func(id): for i in range(0,5): sleep(1) print('thread %d is running %d' % (id,i)) threads = []for i in range(0,3): t = threa

代碼如下:
import threading
from time import sleep
def test_func(id):
for i in range(0,5):
sleep(1)
print('thread %d is running %d' % (id,i))
threads = []
for i in range(0,3):
t = threading.Thread(target=test_func, args=(i,))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
從輸出結果可以看到,3個線程是交替的執行的
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
python創建線程示例
python創建線程示例: 代碼如下:import threadingfrom time import sleep def test_func(id): for i in range(0,5): sleep(1) print('thread %d is running %d' % (id,i)) threads = []for i in range(0,3): t = threa