from concurrent import futures
from multiprocessing import cpu_count
import time
number_list = range(0, 40)
def evaluate_item(x):
# 計算總和涮较,這里只是為了消耗時間
result_item = count(x)
# 打印輸入和輸出結(jié)果
return result_item
def i_o_function(x):
time.sleep(10)
def count(number):
for i in range(0, 100000000):
i = i + 1
return i * number
if __name__ == "__main__":
# 順序執(zhí)行
# start_time = time.time()
# for item in number_list:
# print(i_o_function(item))
# print("Sequential execution in " + str(time.time() - start_time), "seconds")
# 進程池執(zhí)行
# print ("ProcessPoolExecutor workers=3")
# start_time1 = time.time()
# with futures.ProcessPoolExecutor(max_workers=3) as executor:
# future_list = [executor.submit(evaluate_item, item) for item in number_list]
# for future in futures.as_completed(future_list):
# print (future.result())
# print ("Process pool executuin in " + str(time.time() - start_time1) + "seconds")
# print ("ProcessPoolExecutor workers=5")
# start_time1 = time.time()
# with futures.ProcessPoolExecutor(max_workers=5) as executor:
# future_list = [executor.submit(evaluate_item, item) for item in number_list]
# for future in futures.as_completed(future_list):
# print (future.result())
# print ("Process pool executuin in " + str(time.time() - start_time1) + "seconds")
# print ("ProcessPoolExecutor workers=10")
# start_time1 = time.time()
# with futures.ProcessPoolExecutor(max_workers=10) as executor:
# future_list = [executor.submit(evaluate_item, item) for item in number_list]
# for future in futures.as_completed(future_list):
# print (future.result())
# print ("Process pool executuin in " + str(time.time() - start_time1) + "seconds")
# print ("ProcessPoolExecutor workers=%s" % cpu_count())
# start_time1 = time.time()
# with futures.ProcessPoolExecutor() as executor:
# future_list = [executor.submit(i_o_function, item) for item in number_list]
# for future in futures.as_completed(future_list):
# print (future.result())
# print ("Process pool executuin in " + str(time.time() - start_time1) + "seconds")
# 線程池執(zhí)行
start_time2 = time.time()
with futures.ThreadPoolExecutor() as executor:
future_list = [executor.submit(i_o_function, item) for item in number_list]
for future in futures.as_completed(future_list):
print(future.result())
print("Thread pool executuin in " + str(time.time() - start_time2) + "seconds")
start_time2 = time.time()
with futures.ThreadPoolExecutor(max_workers=100000) as executor:
future_list = [executor.submit(i_o_function, item) for item in number_list]
for future in futures.as_completed(future_list):
future.result()
print("Thread pool executuin in " + str(time.time() - start_time2) + "seconds")
python 線程池和進程池(draft)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來之景,“玉大人斤富,你說我怎么就攤上這事《凸罚” “怎么了满力?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長轻纪。 經(jīng)常有香客問我油额,道長,這世上最難降的妖魔是什么刻帚? 我笑而不...
- 正文 為了忘掉前任潦嘶,我火速辦了婚禮,結(jié)果婚禮上崇众,老公的妹妹穿的比我還像新娘掂僵。我一直安慰自己航厚,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布锰蓬。 她就那樣靜靜地躺著幔睬,像睡著了一般。 火紅的嫁衣襯著肌膚如雪芹扭。 梳的紋絲不亂的頭發(fā)上麻顶,一...
- 文/蒼蘭香墨 我猛地睜開眼交胚,長吁一口氣:“原來是場噩夢啊……” “哼份汗!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蝴簇,我...
- 正文 年R本政府宣布,位于F島的核電站箍铭,受9級特大地震影響泊柬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜诈火,卻給世界環(huán)境...
- 文/蒙蒙 一兽赁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦闸氮、人聲如沸剪况。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽译断。三九已至,卻和暖如春或悲,著一層夾襖步出監(jiān)牢的瞬間孙咪,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 一踏拜、創(chuàng)建多線程 Python提供兩個模塊進行多線程的操作碎赢,分別是thread和threading,前者是比較低級的...
- Pool 如果要啟動大量的子進程速梗,可以用進程池的方式批量創(chuàng)建子進程: 對Pool對象調(diào)用join()方法會等待所有...
- 引言 Python標(biāo)準(zhǔn)庫為我們提供了threading和multiprocessing模塊編寫相應(yīng)的多線程/多進程...
- python最易用的并發(fā)處理--multiprocessing.Pool進程池及ThreadPool線程池 使用場...