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

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

        <bdo id='ml4Uv'></bdo><ul id='ml4Uv'></ul>
      <tfoot id='ml4Uv'></tfoot>

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

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

        如何將python dict與多處理同步

        How to synchronize a python dict with multiprocessing(如何將python dict與多處理同步)
        <i id='8Ap0F'><tr id='8Ap0F'><dt id='8Ap0F'><q id='8Ap0F'><span id='8Ap0F'><b id='8Ap0F'><form id='8Ap0F'><ins id='8Ap0F'></ins><ul id='8Ap0F'></ul><sub id='8Ap0F'></sub></form><legend id='8Ap0F'></legend><bdo id='8Ap0F'><pre id='8Ap0F'><center id='8Ap0F'></center></pre></bdo></b><th id='8Ap0F'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='8Ap0F'><tfoot id='8Ap0F'></tfoot><dl id='8Ap0F'><fieldset id='8Ap0F'></fieldset></dl></div>
          <bdo id='8Ap0F'></bdo><ul id='8Ap0F'></ul>

              <tbody id='8Ap0F'></tbody>

            <small id='8Ap0F'></small><noframes id='8Ap0F'>

          1. <tfoot id='8Ap0F'></tfoot>
                <legend id='8Ap0F'><style id='8Ap0F'><dir id='8Ap0F'><q id='8Ap0F'></q></dir></style></legend>
                • 本文介紹了如何將python dict與多處理同步的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在使用 Python 2.6 和多處理模塊進行多線程處理.現在我想要一個同步的字典(我真正需要的唯一原子操作是一個值上的 += 運算符).

                  I am using Python 2.6 and the multiprocessing module for multi-threading. Now I would like to have a synchronized dict (where the only atomic operation I really need is the += operator on a value).

                  我應該用 multiprocessing.sharedctypes.synchronized() 調用來包裝字典嗎?或者是另一種方式?

                  Should I wrap the dict with a multiprocessing.sharedctypes.synchronized() call? Or is another way the way to go?

                  推薦答案

                  簡介

                  似乎有很多扶手椅建議,但沒有工作示例.這里列出的答案都沒有建議使用多處理,這有點令人失望和不安.作為 python 愛好者,我們應該支持我們的內置庫,雖然并行處理和同步從來都不是一件小事,但我相信通過適當的設計可以讓它變得微不足道.這在現代多核架構中變得極為重要,怎么強調都不為過!也就是說,我對多處理庫還很不滿意,因為它仍處于起步階段,有很多陷阱、錯誤,并且面向函數式編程(我討厭).目前,由于多處理在在服務器運行時無法共享新創建的對象.管理器對象的注冊"類方法只會在管理器(或其服務器)啟動之前實際注冊一個對象.廢話不多說,更多代碼:

                  Intro

                  There seems to be a lot of arm-chair suggestions and no working examples. None of the answers listed here even suggest using multiprocessing and this is quite a bit disappointing and disturbing. As python lovers we should support our built-in libraries, and while parallel processing and synchronization is never a trivial matter, I believe it can be made trivial with proper design. This is becoming extremely important in modern multi-core architectures and cannot be stressed enough! That said, I am far from satisfied with the multiprocessing library, as it is still in its infancy stages with quite a few pitfalls, bugs, and being geared towards functional programming (which I detest). Currently I still prefer the Pyro module (which is way ahead of its time) over multiprocessing due to multiprocessing's severe limitation in being unable to share newly created objects while the server is running. The "register" class-method of the manager objects will only actually register an object BEFORE the manager (or its server) is started. Enough chatter, more code:

                  from multiprocessing.managers import SyncManager
                  
                  
                  class MyManager(SyncManager):
                      pass
                  
                  
                  syncdict = {}
                  def get_dict():
                      return syncdict
                  
                  if __name__ == "__main__":
                      MyManager.register("syncdict", get_dict)
                      manager = MyManager(("127.0.0.1", 5000), authkey="password")
                      manager.start()
                      raw_input("Press any key to kill server".center(50, "-"))
                      manager.shutdown()
                  

                  在上面的代碼示例中,Server.py 使用了 multiprocessing 的 SyncManager,它可以提供同步的共享對象.此代碼無法在解釋器中運行,因為多處理庫對于如何找到每個注冊對象的可調用"非常敏感.運行 Server.py 將啟動一個自定義的 SyncManager,該 SyncManager 共享同步字典以供多個進程使用,并且可以連接到同一臺機器上的客戶端,或者如果在環回以外的 IP 地址上運行,則可以連接到其他機器.在這種情況下,服務器在端口 5000 上的環回 (127.0.0.1) 上運行.使用 authkey 參數在操作 syncdict 時使用安全連接.按下任意鍵時,管理器將關閉.

                  In the above code example, Server.py makes use of multiprocessing's SyncManager which can supply synchronized shared objects. This code will not work running in the interpreter because the multiprocessing library is quite touchy on how to find the "callable" for each registered object. Running Server.py will start a customized SyncManager that shares the syncdict dictionary for use of multiple processes and can be connected to clients either on the same machine, or if run on an IP address other than loopback, other machines. In this case the server is run on loopback (127.0.0.1) on port 5000. Using the authkey parameter uses secure connections when manipulating syncdict. When any key is pressed the manager is shutdown.

                  from multiprocessing.managers import SyncManager
                  import sys, time
                  
                  class MyManager(SyncManager):
                      pass
                  
                  MyManager.register("syncdict")
                  
                  if __name__ == "__main__":
                      manager = MyManager(("127.0.0.1", 5000), authkey="password")
                      manager.connect()
                      syncdict = manager.syncdict()
                  
                      print "dict = %s" % (dir(syncdict))
                      key = raw_input("Enter key to update: ")
                      inc = float(raw_input("Enter increment: "))
                      sleep = float(raw_input("Enter sleep time (sec): "))
                  
                      try:
                           #if the key doesn't exist create it
                           if not syncdict.has_key(key):
                               syncdict.update([(key, 0)])
                           #increment key value every sleep seconds
                           #then print syncdict
                           while True:
                                syncdict.update([(key, syncdict.get(key) + inc)])
                                time.sleep(sleep)
                                print "%s" % (syncdict)
                      except KeyboardInterrupt:
                           print "Killed client"
                  

                  客戶端還必須創建一個自定義的 SyncManager,注冊syncdict",這一次不傳入可調用來檢索共享字典.然后,它使用自定義的 SycnManager 使用端口 5000 上的環回 IP 地址 (127.0.0.1) 和在 Server.py 中啟動的與管理器建立安全連接的 authkey 進行連接.它通過調用管理器上注冊的可調用對象來檢索共享字典同步字典.它會提示用戶以下內容:

                  The client must also create a customized SyncManager, registering "syncdict", this time without passing in a callable to retrieve the shared dict. It then uses the customized SycnManager to connect using the loopback IP address (127.0.0.1) on port 5000 and an authkey establishing a secure connection to the manager started in Server.py. It retrieves the shared dict syncdict by calling the registered callable on the manager. It prompts the user for the following:

                  1. syncdict 中要操作的鍵
                  2. 每次循環增加鍵訪問的值的數量
                  3. 每個周期的睡眠時間(以秒為單位)

                  客戶端然后檢查密鑰是否存在.如果不是,它會在同步字典上創建密鑰.客戶端然后進入一個無限"循環,在該循環中它通過增量更新鍵的值,休眠指定的數量,并打印同步字典,僅重復此過程,直到發生 KeyboardInterrupt (Ctrl+C).

                  The client then checks to see if the key exists. If it doesn't it creates the key on the syncdict. The client then enters an "endless" loop where it updates the key's value by the increment, sleeps the amount specified, and prints the syncdict only to repeat this process until a KeyboardInterrupt occurs (Ctrl+C).

                  1. 必須在管理器啟動之前調用管理器的注冊方法,否則即使對管理器的 dir 調用會顯示它確實具有已注冊的方法,也會出現異常.
                  2. 必須使用方法而不是 dict 分配來完成對 dict 的所有操作(由于多處理共享自定義對象的方式,syncdict["blast"] = 2 會慘遭失敗)
                  3. 使用 SyncManager 的 dict 方法可以緩解煩人的問題 #2,但煩人的問題 #1 會阻止 SyncManager.dict() 返回的代理被注冊和共享.(SyncManager.dict() 只能在管理器啟動后調用,并且注冊只會在管理器啟動之前起作用,因此 SyncManager.dict() 僅在進行函數式編程并將代理作為參數傳遞給進程時有用,如文檔示例)
                  4. 服務器和客戶端都必須注冊,盡管從直覺上看,客戶端似乎只能在連接到管理器后才能弄清楚(請將此添加到您的愿望清單多處理開發人員中)

                  結束

                  我希望你和我一樣喜歡這個相當徹底但稍微費時的答案.我很難弄清楚為什么我在多處理模塊上苦苦掙扎,Pyro 讓它變得輕而易舉,現在多虧了這個答案,我已經一針見血了.我希望這對 python 社區如何改進多處理模塊有用,因為我相信它有很大的希望,但在它的起步階段還沒有實現.盡管描述了令人討厭的問題,但我認為這仍然是一個非常可行的選擇并且非常簡單.您也可以使用 SyncManager.dict() 并將其作為參數傳遞給 Processes,就像文檔顯示的那樣,根據您的要求,這可能是一個更簡單的解決方案,這對我來說是不自然的.

                  Closing

                  I hope you enjoyed this quite thorough and slightly time-consuming answer as much as I have. I was having a great deal of trouble getting straight in my mind why I was struggling so much with the multiprocessing module where Pyro makes it a breeze and now thanks to this answer I have hit the nail on the head. I hope this is useful to the python community on how to improve the multiprocessing module as I do believe it has a great deal of promise but in its infancy falls short of what is possible. Despite the annoying problems described I think this is still quite a viable alternative and is pretty simple. You could also use SyncManager.dict() and pass it to Processes as an argument the way the docs show and it would probably be an even simpler solution depending on your requirements it just feels unnatural to me.

                  這篇關于如何將python dict與多處理同步的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='BbYlI'></bdo><ul id='BbYlI'></ul>

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

                    • <tfoot id='BbYlI'></tfoot>
                      <legend id='BbYlI'><style id='BbYlI'><dir id='BbYlI'><q id='BbYlI'></q></dir></style></legend>

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

                          <tbody id='BbYlI'></tbody>
                          • 主站蜘蛛池模板: 欧产日产国产精品视频 | 人干人人 | 中文字幕一区在线 | 91亚洲国产成人久久精品网站 | 亚洲精品久久久久久国产精华液 | 91 在线 | www.夜夜草| 日韩精品免费在线观看 | 成人国产精品久久 | 曰批视频在线观看 | 欧美久久精品一级c片 | 久久久性色精品国产免费观看 | 最新中文字幕一区 | 亚洲综合伊人 | 亚洲精品免费在线观看 | 久久国产精品一区 | aaaaaa大片免费看最大的 | 欧美一区二区三区,视频 | 午夜视频在线观看视频 | 日本三级网站在线 | 久久久av| 欧美日韩久久精品 | 国产亚洲精品精品国产亚洲综合 | 日韩欧美在线视频观看 | 日本福利视频免费观看 | 久久精品国产一区二区 | 日韩一区精品 | 亚洲狠狠丁香婷婷综合久久久 | 在线播放中文字幕 | 国产在线观看 | 久久专区| 午夜精品91 | 综合欧美亚洲 | 日韩精品一区二区三区中文字幕 | 久久国产电影 | 在线中文字幕视频 | 国产精品1区2区 | 亚洲欧美少妇 | 日干夜操| 在线免费激情视频 | 一色桃子av一区二区 |