from concurrent import futures
from multiprocessing import cpu_count
import time
number_list = range(0, 40)
def evaluate_item(x):
# 计算总和,这里只是为了消耗时间
result_item = count(x)
# 打印输入和输出结果
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__":
# 顺序执行
# start_time = time.time()
# for item in number_list:
# print(i_o_function(item))
# print("Sequential execution in " + str(time.time() - start_time), "seconds")
# 进程池执行
# 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")
# 线程池执行
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)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Pool 如果要启动大量的子进程,可以用进程池的方式批量创建子进程: 对Pool对象调用join()方法会等待所有...
- python最易用的并发处理--multiprocessing.Pool进程池及ThreadPool线程池 使用场...