久久久久久久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 返回 {}.我怎樣才能讓它發(fā)揮作用?

                  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() 函數(shù)有兩個參數(shù):一個函數(shù)和一個序列.將使用序列中的連續(xù)值調(diào)用該函數(shù).

                  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 這樣的可變變量中收集值(在示例中,它是參數(shù) y),因為您的代碼將在多個不同的進程中運行.將值寫入另一個進程中的 dict 不會將該值發(fā)送回原始進程.但是如果你使用 Pool.map() 其他進程將返回每個函數(shù)調(diào)用的結果,返回到第一個進程.然后你可以收集這些值來構建一個 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 到某個冪,并且將提供該冪.讓我們讓它接受一個字符串鍵參數(shù).這意味著 f() 需要接受一個元組參數(shù),其中元組將是 (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模板網(wǎng)!

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

                  相關文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數(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 多進程池.當其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數(shù)?)
                  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. 主站蜘蛛池模板: 国产精品久久久久无码av | 国产精品久久久久久中文字 | 日日操夜夜操视频 | 综合激情av | 国产一级一级国产 | 中文字幕亚洲欧美日韩在线不卡 | 国产99精品 | 五月激情婷婷在线 | www.xxxx欧美 | 色综合九九 | 亚洲欧美日韩国产综合 | 亚洲国产精品99久久久久久久久 | 国产羞羞视频在线观看 | 亚洲精品免费在线 | 国产成人午夜精品影院游乐网 | 国产 欧美 日韩 一区 | 伊人网综合在线 | 精品国产乱码久久久久久图片 | 欧产日产国产精品国产 | 久久久久久高潮国产精品视 | 二区不卡 | 欧美成人免费在线视频 | h视频在线观看免费 | 精品三级在线观看 | www.youjizz.com日韩 | 日本特黄a级高清免费大片 成年人黄色小视频 | 99re视频这里只有精品 | 中文字幕视频一区二区 | 国产精品欧美日韩 | 亚洲国产一区视频 | 成人在线视频观看 | 一区二区三区在线免费观看 | 一区二区三区影院 | 一区二区三区四区在线免费观看 | 成人亚洲视频 | 天堂av在线影院 | 久久亚洲精品国产精品紫薇 | 日韩在线电影 | 欧美另类视频在线 | 91精品国产一区二区三区动漫 | 超碰婷婷 |