<legend id='knboA'><style id='knboA'><dir id='knboA'><q id='knboA'></q></dir></style></legend>
      <bdo id='knboA'></bdo><ul id='knboA'></ul>
    <tfoot id='knboA'></tfoot>

        <i id='knboA'><tr id='knboA'><dt id='knboA'><q id='knboA'><span id='knboA'><b id='knboA'><form id='knboA'><ins id='knboA'></ins><ul id='knboA'></ul><sub id='knboA'></sub></form><legend id='knboA'></legend><bdo id='knboA'><pre id='knboA'><center id='knboA'></center></pre></bdo></b><th id='knboA'></th></span></q></dt></tr></i><div id='knboA'><tfoot id='knboA'></tfoot><dl id='knboA'><fieldset id='knboA'></fieldset></dl></div>
      1. <small id='knboA'></small><noframes id='knboA'>

      2. multiprocessing.Pool 示例

        时间:2023-05-26

            <bdo id='6EQ7S'></bdo><ul id='6EQ7S'></ul>

              <i id='6EQ7S'><tr id='6EQ7S'><dt id='6EQ7S'><q id='6EQ7S'><span id='6EQ7S'><b id='6EQ7S'><form id='6EQ7S'><ins id='6EQ7S'></ins><ul id='6EQ7S'></ul><sub id='6EQ7S'></sub></form><legend id='6EQ7S'></legend><bdo id='6EQ7S'><pre id='6EQ7S'><center id='6EQ7S'></center></pre></bdo></b><th id='6EQ7S'></th></span></q></dt></tr></i><div id='6EQ7S'><tfoot id='6EQ7S'></tfoot><dl id='6EQ7S'><fieldset id='6EQ7S'></fieldset></dl></div>
            1. <small id='6EQ7S'></small><noframes id='6EQ7S'>

                <tfoot id='6EQ7S'></tfoot>
                  <tbody id='6EQ7S'></tbody>
                <legend id='6EQ7S'><style id='6EQ7S'><dir id='6EQ7S'><q id='6EQ7S'></q></dir></style></legend>
                • 本文介绍了multiprocessing.Pool 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试学习如何使用 multiprocessing,结果发现 下面的例子.

                  I'm trying to learn how to use multiprocessing, and found the following example.

                  我想对值求和如下:

                  from multiprocessing import Pool
                  from time import time
                  
                  N = 10
                  K = 50
                  w = 0
                  
                  def CostlyFunction(z):
                      r = 0
                      for k in xrange(1, K+2):
                          r += z ** (1 / k**1.5)
                      print r
                      w += r
                      return r
                  
                  currtime = time()
                  
                  po = Pool()
                  
                  for i in xrange(N):
                      po.apply_async(CostlyFunction,(i,))
                  po.close()
                  po.join()
                  
                  print w
                  print '2: parallel: time elapsed:', time() - currtime
                  

                  我无法得到所有 r 值的总和.

                  I can't get the sum of all r values.

                  推荐答案

                  如果你要像这样使用 apply_async,那么你必须使用某种共享内存.此外,您需要放置启动多处理的部分,以便它仅在由初始脚本调用时完成,而不是池进程.这是使用地图的一种方法.

                  If you're going to use apply_async like that, then you have to use some sort of shared memory. Also, you need to put the part that starts the multiprocessing so that it is only done when called by the initial script, not the pooled processes. Here's a way to do it with map.

                  from multiprocessing import Pool
                  from time import time
                  
                  K = 50
                  def CostlyFunction((z,)):
                      r = 0
                      for k in xrange(1, K+2):
                          r += z ** (1 / k**1.5)
                      return r
                  
                  if __name__ == "__main__":
                      currtime = time()
                      N = 10
                      po = Pool()
                      res = po.map_async(CostlyFunction,((i,) for i in xrange(N)))
                      w = sum(res.get())
                      print w
                      print '2: parallel: time elapsed:', time() - currtime
                  

                  这篇关于multiprocessing.Pool 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python多处理池在加入时挂起? 下一篇:如何在 Python 中对类实例使用多处理?

                  相关文章

                  最新文章

                • <legend id='tjO0j'><style id='tjO0j'><dir id='tjO0j'><q id='tjO0j'></q></dir></style></legend>
                  <tfoot id='tjO0j'></tfoot>
                • <small id='tjO0j'></small><noframes id='tjO0j'>

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

                      <i id='tjO0j'><tr id='tjO0j'><dt id='tjO0j'><q id='tjO0j'><span id='tjO0j'><b id='tjO0j'><form id='tjO0j'><ins id='tjO0j'></ins><ul id='tjO0j'></ul><sub id='tjO0j'></sub></form><legend id='tjO0j'></legend><bdo id='tjO0j'><pre id='tjO0j'><center id='tjO0j'></center></pre></bdo></b><th id='tjO0j'></th></span></q></dt></tr></i><div id='tjO0j'><tfoot id='tjO0j'></tfoot><dl id='tjO0j'><fieldset id='tjO0j'></fieldset></dl></div>