Python對同一函數不同參數實現多線程操作

此方法用於爬蟲效果極優


import threading
import time
def testa(n):
	while True:
		time.sleep(1)
		print(n)

#先隔出一秒打印出a,再過一秒打出b
ts = []

for i in range(5):
	exec('t{0} = threading.Thread(target=testa,args=(i,))'.format(i))
	exec('ts.append(t{0})'.format(i))
for s in ts:
    s.start()
print("DONE")

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章