线程池的基本使用
2021-04-07 13:27
标签:一个 pre time() 单线程 process 线程 multi int ESS 线程池的基本使用 标签:一个 pre time() 单线程 process 线程 multi int ESS 原文地址:https://www.cnblogs.com/gerenboke/p/13389081.html# import time
# #使用单线程串行方式执行
# def get_page(str):
# print("正在下载:", str)
# time.sleep(2)
# print("下载成功:", str)
# name_list = ["aa","bb","cc","dd"]
# start_time = time.time()
# for i in range(len(name_list)):
# get_page(name_list[i])
# end_time = time.time()
# print(‘%d second ‘% (end_time-start_time))
import time
#导入线程池模块对应的类
from multiprocessing.dummy import Pool
#使用线程池串行方式执行
start_time = time.time()
def get_page(str):
print("正在下载:", str)
time.sleep(2)
print("下载成功:", str)
name_list = ["aa","bb","cc","dd"]
#实例化一个线程池对象
pool = Pool(4)
#将列表中每一个列表元素传递给get_page进行处理
pool.map(get_page,name_list)
end_time = time.time()
print(end_time-start_time)
# for i in range(len(name_list)):
# get_page(name_list[i])
# end_time = time.time()
# print(‘%d second ‘% (end_time-start_time))
上一篇:线程池在爬虫案例中的应用
下一篇:python验证码识别