当前位置: 首页 > news >正文

哪些企业需要网站建设的公司网站功能性建设有哪些

哪些企业需要网站建设的,公司网站功能性建设有哪些,如何把地图放到自己做的网站上,wordpress植物网站动动发财的小手#xff0c;点个赞吧#xff01; 简介 多亏了 GIL#xff0c;使用多个线程来执行 CPU 密集型任务从来都不是一种选择。随着多核 CPU 的普及#xff0c;Python 提供了一种多处理解决方案来执行 CPU 密集型任务。但是直到现在#xff0c;直接使用多进程相关的… 动动发财的小手点个赞吧 简介 多亏了 GIL使用多个线程来执行 CPU 密集型任务从来都不是一种选择。随着多核 CPU 的普及Python 提供了一种多处理解决方案来执行 CPU 密集型任务。但是直到现在直接使用多进程相关的API还是存在一些问题。 在本文[1]开始之前我们还有一小段代码来帮助演示 import timefrom multiprocessing import Processdef sum_to_num(final_num: int) - int:    start  time.monotonic()    result  0    for i in range(0, final_num1, 1):        result  i    print(fThe method with {final_num} completed in {time.monotonic() - start:.2f} second(s).)    return result 该方法接受一个参数并从 0 开始累加到该参数。打印方法执行时间并返回结果。 多进程存在的问题 def main():    # We initialize the two processes with two parameters, from largest to smallest    process_a  Process(targetsum_to_num, args(200_000_000,))    process_b  Process(targetsum_to_num, args(50_000_000,))    # And then let them start executing    process_a.start()    process_b.start()    # Note that the join method is blocking and gets results sequentially    start_a  time.monotonic()    process_a.join()    print(fProcess_a completed in {time.monotonic() - start_a:.2f} seconds)    # Because when we wait process_a for join. The process_b has joined already.    # so the time counter is 0 seconds.    start_b  time.monotonic()    process_b.join()    print(fProcess_b completed in {time.monotonic() - start_b:.2f} seconds) 如代码所示我们直接创建并启动多个进程调用每个进程的start和join方法。但是这里存在一些问题 join 方法不能返回任务执行的结果。 join 方法阻塞主进程并按顺序执行它。 即使后面的任务比前面的任务执行得更快如下图所示 使用池的问题 如果我们使用multiprocessing.Pool也会存在一些问题 def main():    with Pool() as pool:        result_a  pool.apply(sum_to_num, args(200_000_000,))        result_b  pool.apply(sum_to_num, args(50_000_000,))        print(fsum_to_num with 200_000_000 got a result of {result_a}.)        print(fsum_to_num with 50_000_000 got a result of {result_b}.) 如代码所示Pool 的 apply 方法是同步的这意味着您必须等待之前的 apply 任务完成才能开始执行下一个 apply 任务。 当然我们可以使用 apply_async 方法异步创建任务。但是同样您需要使用 get 方法来阻塞地获取结果。它让我们回到 join 方法的问题 def main():    with Pool() as pool:        result_a  pool.apply_async(sum_to_num, args(200_000_000,))        result_b  pool.apply_async(sum_to_num, args(50_000_000,))        print(fsum_to_num with 200_000_000 got a result of {result_a.get()}.)        print(fsum_to_num with 50_000_000 got a result of {result_b.get()}.) 直接使用ProcessPoolExecutor的问题 那么如果我们使用 concurrent.futures.ProcesssPoolExecutor 来执行我们的 CPU 绑定任务呢 def main():    with ProcessPoolExecutor() as executor:        numbers  [200_000_000, 50_000_000]        for result in executor.map(sum_to_num, numbers):            print(fsum_to_num got a result which is {result}.) 如代码所示一切看起来都很棒并且就像 asyncio.as_completed 一样被调用。但是看看结果它们仍按启动顺序获取。这与 asyncio.as_completed 完全不同后者按照执行顺序获取结果 使用 asyncio 的 run_in_executor 修复 幸运的是我们可以使用 asyncio 来处理 IO-bound 任务它的 run_in_executor 方法可以像 asyncio 一样调用多进程任务。不仅统一了并发和并行的API还解决了我们上面遇到的各种问题 async def main():    loop  asyncio.get_running_loop()    tasks  []    with ProcessPoolExecutor() as executor:        for number in [200_000_000, 50_000_000]:            tasks.append(loop.run_in_executor(executor, sum_to_num, number))                # Or we can just use the method asyncio.gather(*tasks)        for done in asyncio.as_completed(tasks):            result  await done            print(fsum_to_num got a result which is {result}) 由于上一篇的示例代码都是模拟我们应该调用的并发过程的方法所以很多读者在学习之后在实际编码中还是需要帮助理解如何使用。所以在了解了为什么我们需要在asyncio中执行CPU-bound并行任务之后今天我们将通过一个真实世界的例子来解释如何使用asyncio同时处理IO-bound和CPU-bound任务并领略asyncio对我们的效率代码。 Reference [1] Source: https://towardsdatascience.com/combining-multiprocessing-and-asyncio-in-python-for-performance-boosts-15496ffe96b 本文由 mdnice 多平台发布
http://www.yayakq.cn/news/1122/

相关文章:

  • 个人网站网页设计模板临沂设计网站的公司
  • 工信和信息化网站备案系统广州网站开发十度网络最好
  • 英语门户网站织梦源码简洁轻便的wordpress主题
  • 网站开发项目个人总结做的网站怎么发布到网上
  • 网站栏目下拉菜单南昌seo站内优化
  • 南通市网站建设浑南区建设局网站
  • 互联网众筹网站怎样建设如何编写微信小程序代码
  • 网站开发自学时间申请注册商标需要多少钱
  • 自己架设服务器建网站企业数字化建设公司
  • .net做网站教程seo推广优化工具
  • wordpress网站后台代做毕业设计找哪个网站好
  • wordpress多站点模式大数据营销有哪些方面的应用
  • 阿里云个人不能开网站做设计需要素材的常用网站有哪些
  • 本地网站建设官网外贸圈阿里巴巴
  • 南京建设监理协会网站打不开深圳做琴行的公司网站
  • 建立搜索引擎网站网站设计制作一条龙
  • 网站备案有什么作用镇江怎么样
  • 网站一级域名和二级域名区别网络信息安全网站开发教程
  • 专门做特价的网站电子商务网站建设的背景
  • 沧州网站制作校园在线网站怎么做
  • 网站开发设计有限公司电脑中怎样安装wordpress
  • 制作 网站wordpress 前台用户中心
  • 好的网站怎么设计网站营销费用
  • 东城免费做网站网站建设都有哪些方面
  • 阳江做网站seo论文中网站数据如何做脚注
  • 济南腾飞网络网站建设东莞网站建设地点优化
  • 注册个网站怎么注册东营会计信息网官网首页
  • 什么网站可以做高三英语试题客户做网站要退款
  • 做网站怎么做推广石狮app网站开发
  • windows优化大师官网贵阳关键词优化平台