<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 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>

        如何在 Python 中使用多处理并行求和循环

        时间:2023-05-25
          <tfoot id='w2qnu'></tfoot>
          <legend id='w2qnu'><style id='w2qnu'><dir id='w2qnu'><q id='w2qnu'></q></dir></style></legend>
              <tbody id='w2qnu'></tbody>
          1. <small id='w2qnu'></small><noframes id='w2qnu'>

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

                • <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 id='w2qnu'><tfoot id='w2qnu'></tfoot><dl id='w2qnu'><fieldset id='w2qnu'></fieldset></dl></div>
                • 本文介绍了如何在 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模板网!

                  上一篇:gensim LdaMulticore 不是多处理? 下一篇:多处理 - 生产者/消费者设计

                  相关文章

                  最新文章

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

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

                  1. <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 id='pHpKG'><tfoot id='pHpKG'></tfoot><dl id='pHpKG'><fieldset id='pHpKG'></fieldset></dl></div>