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

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

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

      <tfoot id='M2ttX'></tfoot>

      1. Python 在并行進程之間共享字典

        Python sharing a dictionary between parallel processes(Python 在并行進程之間共享字典)

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

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

            • <legend id='MQSb6'><style id='MQSb6'><dir id='MQSb6'><q id='MQSb6'></q></dir></style></legend>

                  本文介紹了Python 在并行進程之間共享字典的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我想在我的進程之間共享一個字典,如下所示:

                  I want to share a dictionary between my processes as follows:

                  def f(y,x):
                      y[x]=[x*x]                                                          
                  
                  if __name__ == '__main__':
                      pool = Pool(processes=4)
                      inputs = range(10)
                      y={}                             
                      result = pool.map(f,y,inputs)
                  

                  y 返回 {}.我怎樣才能讓它發揮作用?

                  The y returns {}. How can I make it work?

                  謝謝,

                  推薦答案

                  這看起來你正在使用 multiprocessing 模塊.你沒有說,這是一個重要的信息.

                  This looks like you are using the multiprocessing module. You didn't say, and that's an important bit of information.

                  multiprocessing.Pool() 實例上的 .map() 函數有兩個參數:一個函數和一個序列.將使用序列中的連續值調用該函數.

                  The .map() function on a multiprocessing.Pool() instance takes two arguments: a function, and a sequence. The function will be called with successive values from the sequence.

                  您不能在像 dict 這樣的可變變量中收集值(在示例中,它是參數 y),因為您的代碼將在多個不同的進程中運行.將值寫入另一個進程中的 dict 不會將該值發送回原始進程.但是如果你使用 Pool.map() 其他進程將返回每個函數調用的結果,返回到第一個進程.然后你可以收集這些值來構建一個 dict.

                  You can't collect values in a mutable like a dict (in the example, it's argument y) because your code will be running in multiple different processes. Writing a value to a dict in another process doesn't send that value back to the original process. But if you use Pool.map() the other processes will return the result from each function call, back to the first process. Then you can collect the values to build a dict.

                  示例代碼:

                  import multiprocessing as mp
                  
                  def f(x):
                      return (x, x*x)
                  
                  if __name__ == '__main__':
                      pool = mp.Pool()
                      inputs = range(10)
                      result = dict(pool.map(f, inputs))
                  

                  result 設置為:{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8:64, 9:81}

                  讓我們改變它,而不是計算 x*x 它將提高 x 到某個冪,并且將提供該冪.讓我們讓它接受一個字符串鍵參數.這意味著 f() 需要接受一個元組參數,其中元組將是 (key, x, p) 并且它將計算 x**p.

                  Let's change it so that instead of computing x*x it will raise x to some power, and the power will be provided. And let's make it take a string key argument. This means that f() needs to take a tuple argument, where the tuple will be (key, x, p) and it will compute x**p.

                  import multiprocessing as mp
                  
                  def f(tup):
                      key, x, p = tup  # unpack tuple into variables
                      return (key, x**p)
                  
                  if __name__ == '__main__':
                      pool = mp.Pool()
                      inputs = range(10)
                      inputs = [("1**1", 1, 1), ("2**2", 2, 2), ("2**3", 2, 3), ("3**3", 3, 3)]
                      result = dict(pool.map(f, inputs))
                  

                  如果您有多個序列,并且需要將它們連接在一起以形成上述的單個序列,請考慮使用 zip()itertools.product.

                  If you have several sequences and you need to join them together to make a single sequence for the above, look into using zip() or perhaps itertools.product.

                  這篇關于Python 在并行進程之間共享字典的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)

                      <tbody id='Lc73D'></tbody>

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

                      <bdo id='Lc73D'></bdo><ul id='Lc73D'></ul>
                    • <tfoot id='Lc73D'></tfoot>

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

                          1. 主站蜘蛛池模板: 午夜视频福利 | 一二区视频 | 久久久夜色精品 | 特级淫片裸体免费看 | 日韩一区二区在线观看视频 | 五月婷婷视频 | 亚洲在线一区 | 国产高清一区二区三区 | 91欧美激情一区二区三区成人 | 中文字幕在线免费观看 | 在线小视频 | 久久久www成人免费精品 | 成年人免费在线视频 | 日韩欧美中文在线 | 久久精品伊人 | 在线播放一区 | 免费成人黄色 | 日韩av不卡在线观看 | 国产综合视频在线观看 | 日本成人久久 | 宅男噜噜噜66一区二区 | 伊人久久综合 | 免费看黄色的视频 | 国产精品成人一区二区三区 | 国产一区在线观看视频 | 久久久久网站 | 黄视频免费在线观看 | 国产精品手机在线观看 | 国产午夜精品一区二区三区视频 | 玖玖在线观看 | www.av在线 | 欧美一区二区三区的 | 亚洲精品免费看 | 国内精品一区二区三区 | 久久久久亚洲精品 | 亚洲不卡 | 国产免费黄色片 | 一级片日韩 | 官场少妇尤物雪白高耸 | 欧美成人免费 | 亚洲天堂国产 |