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

      • <bdo id='vGEPV'></bdo><ul id='vGEPV'></ul>
      1. <legend id='vGEPV'><style id='vGEPV'><dir id='vGEPV'><q id='vGEPV'></q></dir></style></legend>
      2. <tfoot id='vGEPV'></tfoot>
        <i id='vGEPV'><tr id='vGEPV'><dt id='vGEPV'><q id='vGEPV'><span id='vGEPV'><b id='vGEPV'><form id='vGEPV'><ins id='vGEPV'></ins><ul id='vGEPV'></ul><sub id='vGEPV'></sub></form><legend id='vGEPV'></legend><bdo id='vGEPV'><pre id='vGEPV'><center id='vGEPV'></center></pre></bdo></b><th id='vGEPV'></th></span></q></dt></tr></i><div id='vGEPV'><tfoot id='vGEPV'></tfoot><dl id='vGEPV'><fieldset id='vGEPV'></fieldset></dl></div>

        回调函数在多处理 map_async 中如何工作?

        时间:2023-05-27
        • <small id='RBUmZ'></small><noframes id='RBUmZ'>

          • <legend id='RBUmZ'><style id='RBUmZ'><dir id='RBUmZ'><q id='RBUmZ'></q></dir></style></legend>
              <bdo id='RBUmZ'></bdo><ul id='RBUmZ'></ul>
                <tbody id='RBUmZ'></tbody>

                  <tfoot id='RBUmZ'></tfoot>
                1. <i id='RBUmZ'><tr id='RBUmZ'><dt id='RBUmZ'><q id='RBUmZ'><span id='RBUmZ'><b id='RBUmZ'><form id='RBUmZ'><ins id='RBUmZ'></ins><ul id='RBUmZ'></ul><sub id='RBUmZ'></sub></form><legend id='RBUmZ'></legend><bdo id='RBUmZ'><pre id='RBUmZ'><center id='RBUmZ'></center></pre></bdo></b><th id='RBUmZ'></th></span></q></dt></tr></i><div id='RBUmZ'><tfoot id='RBUmZ'></tfoot><dl id='RBUmZ'><fieldset id='RBUmZ'></fieldset></dl></div>
                  本文介绍了回调函数在多处理 map_async 中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  调试代码花了我一晚上的时间,终于发现了这个棘手的问题.请看下面的代码.

                  It cost me a whole night to debug my code, and I finally found this tricky problem. Please take a look at the code below.

                  from multiprocessing import Pool
                  
                  def myfunc(x):
                      return [i for i in range(x)]
                  
                  pool=Pool()
                  
                  A=[]
                  r = pool.map_async(myfunc, (1,2), callback=A.extend)
                  r.wait()
                  

                  我以为我会得到 A=[0,0,1],但输出是 A=[[0],[0,1]].这对我来说没有意义,因为如果我有 A=[]A.extend([0])A.extend([0,1]) 会给我A=[0,0,1].回调可能以不同的方式工作.所以我的问题是如何获得 A=[0,0,1] 而不是 [[0],[0,1]]?

                  I thought I would get A=[0,0,1], but the output is A=[[0],[0,1]]. This does not make sense to me because if I have A=[], A.extend([0]) and A.extend([0,1]) will give me A=[0,0,1]. Probably the callback works in a different way. So my question is how to get A=[0,0,1] instead of [[0],[0,1]]?

                  推荐答案

                  如果使用 map_async,则调用一次回调并返回结果 ([[0], [0, 1]]).

                  Callback is called once with the result ([[0], [0, 1]]) if you use map_async.

                  >>> from multiprocessing import Pool
                  >>> def myfunc(x):
                  ...     return [i for i in range(x)]
                  ... 
                  >>> A = []
                  >>> def mycallback(x):
                  ...     print('mycallback is called with {}'.format(x))
                  ...     A.extend(x)
                  ... 
                  >>> pool=Pool()
                  >>> r = pool.map_async(myfunc, (1,2), callback=mycallback)
                  >>> r.wait()
                  mycallback is called with [[0], [0, 1]]
                  >>> print(A)
                  [[0], [0, 1]]
                  

                  使用 apply_async如果您希望每次都调用回调.

                  Use apply_async if you want callback to be called for each time.

                  pool=Pool()
                  results = []
                  for x in (1,2):
                      r = pool.apply_async(myfunc, (x,), callback=mycallback)
                      results.append(r)
                  for r in results:
                      r.wait()
                  

                  这篇关于回调函数在多处理 map_async 中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我可以在 python 中创建共享的多数组或列表对象列 下一篇:Python multiprocessing.Queue 与 multiprocessing.manager().Qu

                  相关文章

                  最新文章

                  <tfoot id='wGmrU'></tfoot>

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

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

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