发布于2019-08-08 09:56 阅读(937) 评论(0) 点赞(2) 收藏(0)
单线程与多线程性能比较
import requests import time def download_one(url): resp = requests.get(url) print('Read {} from {}'.format(len(resp.content), url)) def download_all(sites): for site in sites: download_one(site) def main(): sites = [ 'https://en.wikipedia.org/wiki/Portal:Arts', 'https://en.wikipedia.org/wiki/Portal:History', 'https://en.wikipedia.org/wiki/Portal:Society', 'https://en.wikipedia.org/wiki/Portal:Biography', 'https://en.wikipedia.org/wiki/Portal:Mathematics', 'https://en.wikipedia.org/wiki/Portal:Technology', 'https://en.wikipedia.org/wiki/Portal:Geography', 'https://en.wikipedia.org/wiki/Portal:Science', 'https://en.wikipedia.org/wiki/Computer_science', 'https://en.wikipedia.org/wiki/Python_(programming_language)', 'https://en.wikipedia.org/wiki/Java_(programming_language)', 'https://en.wikipedia.org/wiki/PHP', 'https://en.wikipedia.org/wiki/Node.js', 'https://en.wikipedia.org/wiki/The_C_Programming_Language', 'https://en.wikipedia.org/wiki/Go_(programming_language)' ] start_time = time.perf_counter() download_all(sites) end_time = time.perf_counter() print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time)) if __name__ == '__main__': main() # 输出 Read 129196 from https://en.wikipedia.org/wiki/Portal:Arts Read 183867 from https://en.wikipedia.org/wiki/Portal:History Read 224161 from https://en.wikipedia.org/wiki/Portal:Society Read 114387 from https://en.wikipedia.org/wiki/Portal:Biography Read 152871 from https://en.wikipedia.org/wiki/Portal:Mathematics Read 156339 from https://en.wikipedia.org/wiki/Portal:Technology Read 162872 from https://en.wikipedia.org/wiki/Portal:Geography Read 91504 from https://en.wikipedia.org/wiki/Portal:Science Read 323262 from https://en.wikipedia.org/wiki/Computer_science Read 391073 from https://en.wikipedia.org/wiki/Python_(programming_language) Read 319710 from https://en.wikipedia.org/wiki/Java_(programming_language) Read 470754 from https://en.wikipedia.org/wiki/PHP Read 180774 from https://en.wikipedia.org/wiki/Node.js Read 56799 from https://en.wikipedia.org/wiki/The_C_Programming_Language Read 325451 from https://en.wikipedia.org/wiki/Go_(programming_language) Download 15 sites in 67.349395015 seconds
import concurrent.futures import requests import threading import time def download_one(url): try: resp = requests.get(url) print('Read {} from {}'.format(len(resp.content), url)) except Exception as ex: print(ex) def download_all(sites): with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: results = executor.map(download_one, sites) # with concurrent.futures.ProcessPoolExecutor() as executor: # results = executor.map(download_one,sites) def main(): sites = [ 'https://en.wikipedia.org/wiki/Portal:Arts', 'https://en.wikipedia.org/wiki/Portal:History', 'https://en.wikipedia.org/wiki/Portal:Society', 'https://en.wikipedia.org/wiki/Portal:Biography', 'https://en.wikipedia.org/wiki/Portal:Mathematics', 'https://en.wikipedia.org/wiki/Portal:Technology', 'https://en.wikipedia.org/wiki/Portal:Geography', 'https://en.wikipedia.org/wiki/Portal:Science', 'https://en.wikipedia.org/wiki/Computer_science', 'https://en.wikipedia.org/wiki/Python_(programming_language)', 'https://en.wikipedia.org/wiki/Java_(programming_language)', 'https://en.wikipedia.org/wiki/PHP', 'https://en.wikipedia.org/wiki/Node.js', 'https://en.wikipedia.org/wiki/The_C_Programming_Language', 'https://en.wikipedia.org/wiki/Go_(programming_language)' ] start_time = time.perf_counter() download_all(sites) end_time = time.perf_counter() print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time)) if __name__ == '__main__': main() # 输出 Read 114387 from https://en.wikipedia.org/wiki/Portal:Biography Read 129196 from https://en.wikipedia.org/wiki/Portal:Arts Read 183867 from https://en.wikipedia.org/wiki/Portal:History Read 152871 from https://en.wikipedia.org/wiki/Portal:Mathematics Read 224161 from https://en.wikipedia.org/wiki/Portal:Society Read 156339 from https://en.wikipedia.org/wiki/Portal:Technology Read 91504 from https://en.wikipedia.org/wiki/Portal:Science Read 391073 from https://en.wikipedia.org/wiki/Python_(programming_language) Read 162872 from https://en.wikipedia.org/wiki/Portal:Geography Read 323262 from https://en.wikipedia.org/wiki/Computer_science Read 56799 from https://en.wikipedia.org/wiki/The_C_Programming_Language Read 319710 from https://en.wikipedia.org/wiki/Java_(programming_language) Read 325451 from https://en.wikipedia.org/wiki/Go_(programming_language) Read 180774 from https://en.wikipedia.org/wiki/Node.js Read 470754 from https://en.wikipedia.org/wiki/PHP Download 15 sites in 10.022916933 seconds
with futures.ThreadPoolExecutor(workers) as executor => with futures.ProcessPoolExecutor() as executor:
对于这种IO场景,用并行的方式并不会比并发的方式效率高.
import concurrent.futures import requests import time def download_one(url): resp = requests.get(url) print('Read {} from {}'.format(len(resp.content), url)) return f'download {len(resp.content)} ok' # def over(arg): # print(arg) # print('over') def download_all(sites): #future列表中每个future完成的顺序,和它在列表中的顺序并不一定完全一致。 #到底哪个先完成、哪个后完成,取决于系统的调度和每个future的执行时间 with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: to_do = [] for site in sites: #executor.submit返回future实例 future = executor.submit(download_one, site) to_do.append(future) #future.add_done_callback(over) #在futures完成后打印结果 for future in concurrent.futures.as_completed(to_do): if future.exception() is not None: print(future.exception()) else: print(future.result()) def main(): sites = [ 'https://en.wikipedia.org/wiki/Portal:Arts', 'https://en.wikipedia.org/wiki/Portal:History', 'https://en.wikipedia.org/wiki/Portal:Society', 'https://en.wikipedia.org/wiki/Portal:Biography', 'https://en.wikipedia.org/wiki/Portal:Mathematics', 'https://en.wikipedia.org/wiki/Portal:Technology', 'https://en.wikipedia.org/wiki/Portal:Geography', 'https://en.wikipedia.org/wiki/Portal:Science', 'https://en.wikipedia.org/wiki/Computer_science', 'https://en.wikipedia.org/wiki/Python_(programming_language)', 'https://en.wikipedia.org/wiki/Java_(programming_language)', 'https://en.wikipedia.org/wiki/PHP', 'https://en.wikipedia.org/wiki/Node.js', 'https://en.wikipedia.org/wiki/The_C_Programming_Language', 'https://en.wikipedia.org/wiki/Go_(programming_language)' ] start_time = time.perf_counter() download_all(sites) end_time = time.perf_counter() print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time)) if __name__ == '__main__': main() # 输出 Read 129886 from https://en.wikipedia.org/wiki/Portal:Arts Read 107634 from https://en.wikipedia.org/wiki/Portal:Biography Read 224118 from https://en.wikipedia.org/wiki/Portal:Society Read 158984 from https://en.wikipedia.org/wiki/Portal:Mathematics Read 184343 from https://en.wikipedia.org/wiki/Portal:History Read 157949 from https://en.wikipedia.org/wiki/Portal:Technology Read 167923 from https://en.wikipedia.org/wiki/Portal:Geography Read 94228 from https://en.wikipedia.org/wiki/Portal:Science Read 391905 from https://en.wikipedia.org/wiki/Python_(programming_language) Read 321352 from https://en.wikipedia.org/wiki/Computer_science Read 180298 from https://en.wikipedia.org/wiki/Node.js Read 321417 from https://en.wikipedia.org/wiki/Java_(programming_language) Read 468421 from https://en.wikipedia.org/wiki/PHP Read 56765 from https://en.wikipedia.org/wiki/The_C_Programming_Language Read 324039 from https://en.wikipedia.org/wiki/Go_(programming_language) Download 15 sites in 0.21698231499976828 seconds
future列表中每个future完成的顺序,和它在列表中的顺序并不一定完全一致。到底哪个先完成、哪个后完成,取决于系统的调度和每个future的执行时间。
并发通常用于 I/O 操作频繁的场景,而并行则适用于 CPU heavy 的场景。
作者:我好看吗
链接:https://www.pythonheidong.com/blog/article/12827/ec9f6dccb5869cd0c97f/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!