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

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

    <legend id='J1WeQ'><style id='J1WeQ'><dir id='J1WeQ'><q id='J1WeQ'></q></dir></style></legend>

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

      <tfoot id='J1WeQ'></tfoot>
      1. 在多处理期间保持统一计数?

        时间:2023-05-26

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

              1. <small id='UEcCR'></small><noframes id='UEcCR'>

                <tfoot id='UEcCR'></tfoot>
                <legend id='UEcCR'><style id='UEcCR'><dir id='UEcCR'><q id='UEcCR'></q></dir></style></legend>
                    <tbody id='UEcCR'></tbody>
                1. 本文介绍了在多处理期间保持统一计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个 python 程序,它运行蒙特卡罗模拟来寻找概率问题的答案.我正在使用多处理,这里是伪代码

                  I have a python program that runs a Monte Carlo simulation to find answers to probability questions. I am using multiprocessing and here it is in pseudo code

                  import multiprocessing
                  
                  def runmycode(result_queue):
                      print "Requested..."
                      while 1==1:
                         iterations +=1
                      if "result found (for example)":
                          result_queue.put("result!")
                  
                      print "Done"
                  
                  processs = []
                  result_queue = multiprocessing.Queue()
                  
                  for n in range(4): # start 4 processes
                      process = multiprocessing.Process(target=runmycode, args=[result_queue])
                      process.start()
                      processs.append(process)
                  
                  print "Waiting for result..."
                  
                  result = result_queue.get() # wait
                  
                  for process in processs: # then kill them all off
                      process.terminate()
                  
                  print "Got result:", result
                  

                  我想对此进行扩展,以便统一计算已运行的迭代次数.就像如果线程 1 已经运行了 100 次,线程 2 已经运行了 100 次,那么我想总共显示 200 次迭代,作为控制台的打印.我指的是线程进程中的 iterations 变量.如何确保所有线程都添加到同一个变量?我认为使用 iterationsGlobal 版本会起作用,但事实并非如此.

                  I'd like to extend this so that I can keep a unified count of the number of iterations that have been run. Like if thread 1 has run 100 times and thread 2 has run 100 times then I want to show 200 iterations total, as a print to the console. I am referring to the iterations variable in the thread process. How can I make sure that ALL threads are adding to the same variable? I thought that using a Global version of iterations would work but it does not.

                  推荐答案

                  正常的全局变量在进程之间的共享方式与线程之间的共享方式不同.您需要使用流程感知数据结构.对于您的用例,multiprocessing.Value 应该可以正常工作:

                  Normal global variables are not shared between processes the way they are shared between threads. You need to use a process-aware data structure. For your use-case, a multiprocessing.Value should work fine:

                  import multiprocessing
                  
                  def runmycode(result_queue, iterations):
                     print("Requested...")
                     while 1==1: # This is an infinite loop, so I assume you want something else here
                         with iterations.get_lock(): # Need a lock because incrementing isn't atomic
                             iterations.value += 1
                     if "result found (for example)":
                         result_queue.put("result!")
                  
                     print("Done")
                  
                  
                  if __name__ == "__main__":
                      processs = []
                      result_queue = multiprocessing.Queue()
                  
                      iterations = multiprocessing.Value('i', 0)
                      for n in range(4): # start 4 processes
                          process = multiprocessing.Process(target=runmycode, args=(result_queue, iterations))
                          process.start()
                          processs.append(process)
                  
                      print("Waiting for result...")
                  
                      result = result_queue.get() # wait
                  
                      for process in processs: # then kill them all off
                          process.terminate()
                  
                      print("Got result: {}".format(result))
                      print("Total iterations {}".format(iterations.value))
                  

                  几点说明:

                  1. 我明确地将 Value 传递给孩子,以保持代码与 Windows 兼容,Windows 无法在父子之间共享读/写全局变量.
                  2. 我用锁保护了增量,因为它不是原子操作,并且容易受到竞争条件的影响.
                  3. 我添加了一个 if __name__ == "__main__": 保护,再次帮助提高 Windows 兼容性,并作为一般最佳实践.
                  1. I explicitly passed the Value to the children, to keep the code compatible with Windows, which can't share read/write global variables between parent and children.
                  2. I protected the increment with a lock, because its not an atomic operation, and is susceptible to race conditions.
                  3. I added an if __name__ == "__main__": guard, again to help with Windows compatibility, and just as a general best practice.

                  这篇关于在多处理期间保持统一计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 2.7:如何弥补缺少的 pool.starmap? 下一篇:故意在python中创建一个孤儿进程

                  相关文章

                  最新文章

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

                  1. <small id='p1F1Z'></small><noframes id='p1F1Z'>

                    <legend id='p1F1Z'><style id='p1F1Z'><dir id='p1F1Z'><q id='p1F1Z'></q></dir></style></legend>
                    1. <tfoot id='p1F1Z'></tfoot>

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