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

        • <bdo id='QoHxm'></bdo><ul id='QoHxm'></ul>
      1. <small id='QoHxm'></small><noframes id='QoHxm'>

        Python多处理附加列表

        时间:2023-05-26
          <bdo id='YU6NN'></bdo><ul id='YU6NN'></ul>
          <tfoot id='YU6NN'></tfoot>
              <tbody id='YU6NN'></tbody>
            <i id='YU6NN'><tr id='YU6NN'><dt id='YU6NN'><q id='YU6NN'><span id='YU6NN'><b id='YU6NN'><form id='YU6NN'><ins id='YU6NN'></ins><ul id='YU6NN'></ul><sub id='YU6NN'></sub></form><legend id='YU6NN'></legend><bdo id='YU6NN'><pre id='YU6NN'><center id='YU6NN'></center></pre></bdo></b><th id='YU6NN'></th></span></q></dt></tr></i><div id='YU6NN'><tfoot id='YU6NN'></tfoot><dl id='YU6NN'><fieldset id='YU6NN'></fieldset></dl></div>
            • <small id='YU6NN'></small><noframes id='YU6NN'>

                1. <legend id='YU6NN'><style id='YU6NN'><dir id='YU6NN'><q id='YU6NN'></q></dir></style></legend>
                  本文介绍了Python多处理附加列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  关于使用 Multiprocessing.Pool() 在多个进程之间共享变量的问题.

                  Have a quick question about a shared variable between multiple processes using Multiprocessing.Pool().

                  如果我从多个进程中更新全局列表,我会遇到任何问题吗?IE.如果两个进程同时尝试更新列表.

                  Will I run in to any issues if I am updating a global list from within multiple processes? I.e. if two of the processes were to try to update the list at the same time.

                  我看过关于在类似事情上使用锁的文档,但我想知道是否有必要.

                  I have seen documentation about using a Lock for similar things but I was wondering if it was necessary.

                  我共享这个变量的方式是在我的回调函数中使用一个全局变量,'success' 在目标函数完成后,我将所有成功的操作附加到:

                  The way I am sharing this variable is by using a global variable in my callback function, 'successes' in which i append all of the successful actions to after the target function has completed:

                  TOTAL_SUCCESSES = []
                  
                  def func(inputs):
                      successes = []
                  
                      for input in inputs:
                          result = #something with return code
                          if result == 0:
                              successes.append(input)
                      return successes
                  
                  def callback(successes):
                      global TOTAL_SUCCESSES
                  
                      for entry in successes:
                          TOTAL_SUCCESSES.append(entry)
                  
                  def main():     
                      pool = mp.Pool()
                      for entry in myInputs:
                           pool.apply_async(func, args=(entry,),callback=callback)         
                  

                  为任何语法错误道歉,很快就写出来了,但是程序正在运行,只是想知道如果我有问题我是否添加了共享变量.

                  Apologize for any syntax errors, wrote this up quickly but the program is working just wondering if I add the shared variable if I will have issues.

                  提前致谢!

                  推荐答案

                  使用您当前的代码,您实际上并没有在进程之间共享 CURRENT_SUCCESSES.callback 在主进程中的结果处理线程中执行.只有一个结果处理线程,因此每个 callback 将一次运行一个,而不是同时运行.所以你写的代码是进程/线程安全的.

                  With your current code, you're not actually sharing CURRENT_SUCCESSES between processes. callback is executed in the main process, in a result handling thread. There is only one result handling thread, so each callback will be run one at a time, not concurrently. So your code as written is process/thread safe.

                  但是,您忘记从 func 中返回 success,这是您想要修复的.

                  However, you are forgetting to return successes from func, which you'll want to fix.

                  此外,使用 map 可以更简洁地编写:

                  Also, this could be much more succinctly written using map:

                  def func(inputs):
                      successes = []
                  
                      for input in inputs:
                          result = #something with return code
                          if result == 0:
                              successes.append(input)
                      return successes
                  
                  def main():     
                      pool = mp.Pool()
                      total_successes = pool.map(func, myInputs) # Returns a list of lists
                      # Flatten the list of lists
                      total_successes = [ent for sublist in total_successes for ent in sublist]
                  

                  这篇关于Python多处理附加列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Python 中,我如何知道一个进程何时完成? 下一篇:multiprocessing.value 语法清晰吗?

                  相关文章

                  最新文章

                  1. <tfoot id='OxTMg'></tfoot>

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

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

                      <bdo id='OxTMg'></bdo><ul id='OxTMg'></ul>
                  2. <legend id='OxTMg'><style id='OxTMg'><dir id='OxTMg'><q id='OxTMg'></q></dir></style></legend>