久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <tfoot id='w2qnu'></tfoot>
    <legend id='w2qnu'><style id='w2qnu'><dir id='w2qnu'><q id='w2qnu'></q></dir></style></legend>
    1. <small id='w2qnu'></small><noframes id='w2qnu'>

          <bdo id='w2qnu'></bdo><ul id='w2qnu'></ul>

      1. <i id='w2qnu'><tr id='w2qnu'><dt id='w2qnu'><q id='w2qnu'><span id='w2qnu'><b id='w2qnu'><form id='w2qnu'><ins id='w2qnu'></ins><ul id='w2qnu'></ul><sub id='w2qnu'></sub></form><legend id='w2qnu'></legend><bdo id='w2qnu'><pre id='w2qnu'><center id='w2qnu'></center></pre></bdo></b><th id='w2qnu'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='w2qnu'><tfoot id='w2qnu'></tfoot><dl id='w2qnu'><fieldset id='w2qnu'></fieldset></dl></div>
      2. 如何在 Python 中使用多處理并行求和循環

        How to parallel sum a loop using multiprocessing in Python(如何在 Python 中使用多處理并行求和循環)

        <small id='rNfmn'></small><noframes id='rNfmn'>

              <bdo id='rNfmn'></bdo><ul id='rNfmn'></ul>

                <tfoot id='rNfmn'></tfoot>
                1. <i id='rNfmn'><tr id='rNfmn'><dt id='rNfmn'><q id='rNfmn'><span id='rNfmn'><b id='rNfmn'><form id='rNfmn'><ins id='rNfmn'></ins><ul id='rNfmn'></ul><sub id='rNfmn'></sub></form><legend id='rNfmn'></legend><bdo id='rNfmn'><pre id='rNfmn'><center id='rNfmn'></center></pre></bdo></b><th id='rNfmn'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='rNfmn'><tfoot id='rNfmn'></tfoot><dl id='rNfmn'><fieldset id='rNfmn'></fieldset></dl></div>
                  <legend id='rNfmn'><style id='rNfmn'><dir id='rNfmn'><q id='rNfmn'></q></dir></style></legend>
                    <tbody id='rNfmn'></tbody>
                  本文介紹了如何在 Python 中使用多處理并行求和循環的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我很難理解如何使用 Python 的多處理模塊.

                  I am having difficulty understanding how to use Python's multiprocessing module.

                  我有一個從 1n 的總和,其中 n=10^10,太大而無法放入列表中,這似乎是許多使用多處理的在線示例的主旨.

                  I have a sum from 1 to n where n=10^10, which is too large to fit into a list, which seems to be the thrust of many examples online using multiprocessing.

                  有沒有辦法將范圍拆分"成一定大小的段,然后對每個段進行求和?

                  Is there a way to "split up" the range into segments of a certain size and then perform the sum for each segment?

                  例如

                  def sum_nums(low,high):
                      result = 0
                      for i in range(low,high+1):
                          result += i
                      return result
                  

                  我想通過將 sum_nums(1,10**10) 分解為許多 sum_nums(1,1000) + sum_nums(1001,2000) + sum_nums(2001) 來計算,3000)... 等等.我知道有一個接近形式的 n(n+1)/2 但假裝我們不知道.

                  And I want to compute sum_nums(1,10**10) by breaking it up into many sum_nums(1,1000) + sum_nums(1001,2000) + sum_nums(2001,3000)... and so on. I know there is a close-form n(n+1)/2 but pretend we don't know that.

                  這是我嘗試過的

                  import multiprocessing
                  
                  def sum_nums(low,high):
                      result = 0
                      for i in range(low,high+1):
                          result += i
                      return result
                  
                  if __name__ == "__main__":
                      n = 1000 
                      procs = 2 
                  
                      sizeSegment = n/procs
                  
                      jobs = []
                      for i in range(0, procs):
                          process = multiprocessing.Process(target=sum_nums, args=(i*sizeSegment+1, (i+1)*sizeSegment))
                          jobs.append(process)
                  
                      for j in jobs:
                          j.start()
                      for j in jobs:
                          j.join()
                  
                      #where is the result?
                  

                  推薦答案

                  首先,解決內存問題的最佳方法是使用迭代器/生成器而不是列表:

                  First, the best way to get around the memory issue is to use an iterator/generator instead of a list:

                  def sum_nums(low, high):
                      result = 0
                      for i in xrange(low, high+1):
                          result += 1
                      return result
                  

                  在python3中,range()產生一個迭代器,所以這只在python2中需要

                  現在,當您希望將處理拆分到不同的進程或 CPU 內核時,多處理就派上用場了.如果您不需要控制單個工作人員,那么最簡單的方法是使用進程池.這將允許您將函數映射到池并獲取輸出.您也可以使用 apply_async 一次將作業應用到池中,并獲得延遲結果,您可以使用 .get():

                  Now, where multiprocessing comes in is when you want to split up the processing to different processes or CPU cores. If you don't need to control the individual workers than the easiest method is to use a process pool. This will let you map a function to the pool and get the output. You can alternatively use apply_async to apply jobs to the pool one at a time and get a delayed result which you can get with .get():

                  import multiprocessing
                  from multiprocessing import Pool
                  from time import time
                  
                  def sum_nums(low, high):
                      result = 0
                      for i in xrange(low, high+1):
                          result += i
                      return result
                  
                  # map requires a function to handle a single argument
                  def sn((low,high)):
                      return sum_nums(low, high) 
                  
                  if __name__ == '__main__': 
                      #t = time()
                      # takes forever   
                      #print sum_nums(1,10**10)
                      #print '{} s'.format(time() -t)
                      p = Pool(4)
                  
                      n = int(1e8)
                      r = range(0,10**10+1,n)
                      results = []
                  
                      # using apply_async
                      t = time()
                      for arg in zip([x+1 for x in r],r[1:]):
                          results.append(p.apply_async(sum_nums, arg))
                  
                      # wait for results
                      print sum(res.get() for res in results)
                      print '{} s'.format(time() -t)
                  
                      # using process pool
                      t = time()
                      print sum(p.map(sn, zip([x+1 for x in r], r[1:])))
                      print '{} s'.format(time() -t)
                  

                  在我的機器上,僅使用 10**10 調用 sum_nums 需要將近 9 分鐘,但使用 Pool(8)n=int(1e8) 將這個時間縮短到一分鐘多一點.

                  On my machine, just calling sum_nums with 10**10 takes almost 9 minutes, but using a Pool(8) and n=int(1e8) reduces this to just over a minute.

                  這篇關于如何在 Python 中使用多處理并行求和循環的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數傳遞給 pool.map() 函數)
                  multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEncodingError: TypeError(cannot serialize _io.BufferedReader object,)) - IT屋-程序員軟件開
                  Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多進程池.當其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數?)
                  yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯誤的另一個混淆,“模塊對象沒有屬性“f)
                    <bdo id='pHpKG'></bdo><ul id='pHpKG'></ul>

                    <small id='pHpKG'></small><noframes id='pHpKG'>

                          <tbody id='pHpKG'></tbody>
                      • <legend id='pHpKG'><style id='pHpKG'><dir id='pHpKG'><q id='pHpKG'></q></dir></style></legend>

                        1. <tfoot id='pHpKG'></tfoot>
                        2. <i id='pHpKG'><tr id='pHpKG'><dt id='pHpKG'><q id='pHpKG'><span id='pHpKG'><b id='pHpKG'><form id='pHpKG'><ins id='pHpKG'></ins><ul id='pHpKG'></ul><sub id='pHpKG'></sub></form><legend id='pHpKG'></legend><bdo id='pHpKG'><pre id='pHpKG'><center id='pHpKG'></center></pre></bdo></b><th id='pHpKG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='pHpKG'><tfoot id='pHpKG'></tfoot><dl id='pHpKG'><fieldset id='pHpKG'></fieldset></dl></div>
                            主站蜘蛛池模板: 日韩欧美精品一区 | 男女免费观看在线爽爽爽视频 | 99在线免费视频 | 高清视频一区二区三区 | 国产精品国产三级国产aⅴ无密码 | 一区福利视频 | 成人av免费| 九九av| 中文字幕1区 | 日日干日日色 | www国产亚洲精品久久网站 | 国产综合精品一区二区三区 | 精品国产精品国产偷麻豆 | 日本免费黄色一级片 | 国产精品不卡一区 | 久久久久成人精品 | 狠狠操你 | 亚洲欧美日韩一区 | 一级黄色片免费在线观看 | 欧美成人猛片aaaaaaa | 精品一区二区三区91 | 国产黄色在线观看 | www.五月天婷婷.com | 国产黄色在线观看 | 欧美操操操 | 精品91久久| 一级毛片在线视频 | 欧美激情久久久 | 国产精品久久久久一区二区三区 | 中文字幕在线国产 | 欧美精品久久久久 | 国产在线观看一区二区三区 | 伊人网99 | 国产三级大片 | 嫩草最新网址 | 久久久久久国产精品免费免费男同 | 91视频在线网站 | 日韩欧美在线视频 | 精品国产一区二区三区久久久四川 | 黄色一级毛片免费看 | 91精品麻豆日日躁夜夜躁 |