久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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 class="qwawimqqmiuu" 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>
      1. 在多處理期間保持統一計數?

        Keep unified count during multiprocessing?(在多處理期間保持統一計數?)
      2. <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 class="qwawimqqmiuu" 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>
          <tbody id='J1WeQ'></tbody>

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

              <tfoot id='J1WeQ'></tfoot>
                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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數傳遞給 pool.map() 函數)
                  multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEncodingError: TypeError(cannot serialize _io.BufferedReader object,)) - IT屋-程序員軟件開
                  Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多進程池.當其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數?)
                  yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯誤的另一個混淆,“模塊對象沒有屬性“f)
                    <bdo id='p1F1Z'></bdo><ul id='p1F1Z'></ul>

                        <tbody id='p1F1Z'></tbody>

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

                      <legend id='p1F1Z'><style id='p1F1Z'><dir id='p1F1Z'><q id='p1F1Z'></q></dir></style></legend>
                        • <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 class="qwawimqqmiuu" id='p1F1Z'><tfoot id='p1F1Z'></tfoot><dl id='p1F1Z'><fieldset id='p1F1Z'></fieldset></dl></div>
                          • 主站蜘蛛池模板: 欧美成人一区二区三区 | 国产精品一区二区三区免费观看 | 密室大逃脱第六季大神版在线观看 | aaa天堂 | 91观看| 午夜视频一区二区 | 国产精选一区 | 亚洲视频在线免费观看 | 久久久久久高潮国产精品视 | 91免费观看视频 | 日韩在线中文 | 成人影 | 中文字幕日本一区二区 | 亚洲 欧美 日韩在线 | 欧美涩| av免费看在线| 日本福利在线观看 | 亚洲日韩中文字幕 | 国产欧美一级二级三级在线视频 | 日韩在线国产精品 | 亚洲成人av在线 | 国产精品色av | 国产欧美精品一区二区 | 美女国内精品自产拍在线播放 | 精品亚洲一区二区三区 | www.色午夜.com| 中文字幕亚洲在线 | 亚洲国产精品久久久久 | 久久精品一区二区三区四区 | 伊人免费视频二 | 久久亚洲天堂 | 一区精品视频在线观看 | 免费观看a级毛片在线播放 黄网站免费入口 | 久久久久网站 | 91精品国产91久久久久游泳池 | 国产精品日韩欧美一区二区三区 | 成人综合在线视频 | 日本在线你懂的 | 国产精品久久久久久久岛一牛影视 | 久热免费 | 国产一区二区成人 |