当前位置:   article > 正文

Python并发之Asyncio_python asyncio

python asyncio
  1. import asyncio
  2. import aiohttp
  3. import time
  4. import concurrent.futures
  5. import multiprocessing
  6. # 异步网页下载
  7. async def download_one(url):
  8. async with aiohttp.ClientSession() as session:
  9. async with session.get(url) as resp:
  10. print("read {} from {}".format(resp.content_length, url))
  11. async def download_all(sites):
  12. tasks = [asyncio.create_task(download_one(site)) for site in sites]
  13. await asyncio.gather(*tasks)
  14. # 求列表中元素的整数平方和 常规版本
  15. def cpu_bound(number):
  16. print("number={}, result={}".format(number, sum(i*i for i in range(number))))
  17. def calculate_sums(numbers):
  18. for number in numbers:
  19. cpu_bound(number)
  20. def calcuter_normal(numbers):
  21. start_time = time.perf_counter()
  22. calculate_sums(numbers)
  23. end_time = time.perf_counter()
  24. print("普通版本,耗时{}秒".format(end_time-start_time))
  25. # 并行版本
  26. def calculate_sums_future(numbers):
  27. with concurrent.futures.ThreadPoolExecutor() as executor:
  28. executor.map(cpu_bound, numbers)
  29. def calcuter_future(numbers):
  30. start_time = time.perf_counter()
  31. calculate_sums_future(numbers)
  32. end_time = time.perf_counter()
  33. print("多进程版本,耗时{}秒".format(end_time-start_time))
  34. # 动态规划版本
  35. squ = {} # 用来储存中间结果
  36. def cpu_dp(number):
  37. result = 0
  38. for i in range(number):
  39. if i not in squ.keys():
  40. squ[i] = i*i
  41. result += squ[i]
  42. print("number={}, result={}".format(number, result))
  43. def calculate_sums_dp(numbers):
  44. for number in numbers:
  45. cpu_dp(number)
  46. def calcuter_dp(numbers):
  47. start_time = time.perf_counter()
  48. calculate_sums_dp(numbers)
  49. end_time = time.perf_counter()
  50. print("动态规划版本,耗时{}秒".format(end_time-start_time))
  51. # multiprocessing版本
  52. def calculate_sums_multiprocessing(numbers):
  53. with multiprocessing.Pool() as pool:
  54. pool.map(cpu_bound, numbers)
  55. def calcuter_multiprocessing(numbers):
  56. start_time = time.perf_counter()
  57. calculate_sums_multiprocessing(numbers)
  58. end_time = time.perf_counter()
  59. print("multiprocessing版本,耗时{}秒".format(end_time-start_time))
  60. if __name__ == "__main__":
  61. sites = [
  62. 'http://www.dapenti.com/blog/readforwx.asp?name=xilei&id=143655',
  63. 'http://www.dapenti.com/blog/readforwx.asp?name=xilei&id=143656',
  64. 'http://www.dapenti.com/blog/readforwx.asp?name=xilei&id=143657',
  65. 'http://www.dapenti.com/blog/readforwx.asp?name=xilei&id=143658',
  66. 'http://www.dapenti.com/blog/readforwx.asp?name=xilei&id=143659',
  67. 'http://www.dapenti.com/blog/readforwx.asp?name=xilei&id=143660',
  68. 'http://www.dapenti.com/blog/readforwx.asp?name=xilei&id=143661',
  69. 'http://www.dapenti.com/blog/readforwx.asp?name=xilei&id=143662'
  70. ]
  71. start_time = time.perf_counter()
  72. asyncio.run(download_all(sites))
  73. end_time = time.perf_counter()
  74. print("异步版下载了{}个网站,耗时{}".format(len(sites), end_time-start_time))
  75. # 思考题
  76. numbers = [1000000+x for x in range(10)]
  77. calcuter_normal(numbers)
  78. calcuter_future(numbers)
  79. calcuter_dp(numbers)
  80. calcuter_multiprocessing(numbers)

END

----------------------------------------------------------------

欢迎关注我的公众号!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/832795
推荐阅读
相关标签
  

闽ICP备14008679号