pythonfor多線程
發布時間: 2025-09-14 05:53:56
㈠ python如何並列運行兩個for循環
需要用到多線程
#!/usr/bin/python3
import _thread
import time
# 為線程定義一個函數
def print_time( threadName, delay):
....count = 0
....while count < 5:
........time.sleep(delay)
........count += 1
........print ("%s: %s" % ( threadName, time.ctime(time.time()) ))
# 創建兩個線程
try:
...._thread.start_new_thread( print_time, ("Thread-1", 2, ) )
...._thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
....print ("Error: 無法啟動線程")
while 1:
....time.sleep(5)
....print('is main ')
熱點內容