久久久久久久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. 在多處理期間保持統(tǒng)一計(jì)數(shù)?

        Keep unified count during multiprocessing?(在多處理期間保持統(tǒng)一計(jì)數(shù)?)
      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. 本文介紹了在多處理期間保持統(tǒng)一計(jì)數(shù)?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時(shí)送ChatGPT賬號(hào)..

                  我有一個(gè) python 程序,它運(yùn)行蒙特卡羅模擬來尋找概率問題的答案.我正在使用多處理,這里是偽代碼

                  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
                  

                  我想對(duì)此進(jìn)行擴(kuò)展,以便統(tǒng)一計(jì)算已運(yùn)行的迭代次數(shù).就像如果線程 1 已經(jīng)運(yùn)行了 100 次,線程 2 已經(jīng)運(yùn)行了 100 次,那么我想總共顯示 200 次迭代,作為控制臺(tái)的打印.我指的是線程進(jìn)程中的 iterations 變量.如何確保所有線程都添加到同一個(gè)變量?我認(rèn)為使用 iterationsGlobal 版本會(huì)起作用,但事實(shí)并非如此.

                  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.

                  推薦答案

                  正常的全局變量在進(jìn)程之間的共享方式與線程之間的共享方式不同.您需要使用流程感知數(shù)據(jù)結(jié)構(gòu).對(duì)于您的用例,multiprocessing.Value 應(yīng)該可以正常工作:

                  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))
                  

                  幾點(diǎn)說明:

                  1. 我明確地將 Value 傳遞給孩子,以保持代碼與 Windows 兼容,Windows 無法在父子之間共享讀/寫全局變量.
                  2. 我用鎖保護(hù)了增量,因?yàn)樗皇窃硬僮鳎⑶胰菀资艿礁?jìng)爭(zhēng)條件的影響.
                  3. 我添加了一個(gè) if __name__ == "__main__": 保護(hù),再次幫助提高 Windows 兼容性,并作為一般最佳實(shí)踐.
                  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.

                  這篇關(guān)于在多處理期間保持統(tǒng)一計(jì)數(shù)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個(gè)參數(shù)傳遞給 pool.map() 函數(shù))
                  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 多進(jìn)程池.當(dāng)其中一個(gè)工作進(jìn)程確定不再需要完成工作時(shí),如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊(duì)列引用傳遞給 pool.map_async() 管理的函數(shù)?)
                  yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯(cuò)誤的另一個(gè)混淆,“模塊對(duì)象沒有屬性“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>
                          • 主站蜘蛛池模板: 四虎毛片| 亚洲午夜视频 | 欧美亚洲视频 | 午夜在线观看视频网站 | 二区三区在线观看 | 在线免费黄色网址 | 一级片在线播放 | 国产中文字幕av | 户外少妇对白啪啪野战 | 国产精品成人在线 | 欧美久久久久久久 | 秘密爱大尺度做爰呻吟 | 国产欧美精品一区二区色综合 | 在线观看中文字幕 | av网在线观看 | 欧美福利视频 | 国产黄色三级 | 日韩天堂网 | 欧美xxxx性 | 中文字幕一二三四区 | 国产伦精品一区二区三区在线 | 亚洲久久久久久 | 人人射人人 | 蜜桃精品噜噜噜成人av | 国产综合视频在线观看 | cao在线 | 成人h视频在线观看 | 中文字幕1区 | 99在线免费观看 | 黄色小说视频网站 | 国产色视频一区二区三区qq号 | 美女一级片 | 欧美一级色 | 日韩高清中文字幕 | 免费在线毛片 | 国产精品777 | 成年免费视频黄网站在线观看 | 不卡av网站| 男人午夜影院 | 91视频日本 | 久久视频免费在线观看 |