python多線程

import time
from concurrent.futures import ThreadPoolExecutor

def test(name):
print "write to db " + name

name_list = ['Lucy', 'Lily', 'Tom', 'Lucy2', 'Lily2', 'Tom2', 'Lucy3', 'Lily3', 'Tom3']

#start = time.time()
#for i in range(9):
#test(name_list[i])
#end = time.time()
#print end - start # 7.00950622559e-05

pool = ThreadPoolExecutor(max_workers=3)
start = time.time()
for i in range(9):
test(name_list[i])
pool.submit(test, name_list[i])
pool.shutdown(wait=True) # pool.shutdown(wait=True)是進程池內部的進程都執行完畢,纔會關閉,然後執行後續代碼
end = time.time()
print end - start # 0.0054759979248

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