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

    <tfoot id='Bvq09'></tfoot>

      <legend id='Bvq09'><style id='Bvq09'><dir id='Bvq09'><q id='Bvq09'></q></dir></style></legend>

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

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

      • <bdo id='Bvq09'></bdo><ul id='Bvq09'></ul>

        如何在進程之間共享一個類?

        How can I share a class between processes?(如何在進程之間共享一個類?)
      1. <i id='MC5mn'><tr id='MC5mn'><dt id='MC5mn'><q id='MC5mn'><span id='MC5mn'><b id='MC5mn'><form id='MC5mn'><ins id='MC5mn'></ins><ul id='MC5mn'></ul><sub id='MC5mn'></sub></form><legend id='MC5mn'></legend><bdo id='MC5mn'><pre id='MC5mn'><center id='MC5mn'></center></pre></bdo></b><th id='MC5mn'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='MC5mn'><tfoot id='MC5mn'></tfoot><dl id='MC5mn'><fieldset id='MC5mn'></fieldset></dl></div>
            1. <tfoot id='MC5mn'></tfoot>
                <tbody id='MC5mn'></tbody>
              1. <small id='MC5mn'></small><noframes id='MC5mn'>

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

                  <legend id='MC5mn'><style id='MC5mn'><dir id='MC5mn'><q id='MC5mn'></q></dir></style></legend>

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

                  問題描述

                  限時送ChatGPT賬號..

                  我想要一個由所有進程共享和更新的全局對象,并且鎖定最少.

                  I want to have global object which is shared and updated by all processes with minimum locking.

                  import multiprocessing
                  
                  class Counter(object):
                    def __init__(self):
                      self.value = 0
                  
                    def update(self, value):
                      self.value += value
                  
                  
                  def update(counter_proxy, thread_id):
                    counter_proxy.value.update(1)
                    print counter_proxy.value.value, 't%s' % thread_id, 
                      multiprocessing.current_process().name
                    return counter_proxy.value.value
                  
                  def main():
                    manager = multiprocessing.Manager()
                    counter = manager.Value(Counter, Counter())
                    pool = multiprocessing.Pool(multiprocessing.cpu_count())
                    for i in range(10):
                      pool.apply(func = update, args = (counter, i))
                    pool.close()
                    pool.join()
                  
                    print 'Should be 10 but is %s.' % counter.value.value
                  
                  if __name__ == '__main__':
                    main()
                  

                  結果是這個 - 不是 10 而是零.看起來對象的共享值沒有更新.如何鎖定和更新這樣的值?

                  The result is this - not 10 but zero. It looks like the object's shared value is not updated. How can I lock and update such value?

                  0 t0 PoolWorker-2
                  0 t1 PoolWorker-3
                  0 t2 PoolWorker-5
                  0 t3 PoolWorker-8
                  0 t4 PoolWorker-9
                  0 t5 PoolWorker-2
                  0 t6 PoolWorker-7
                  0 t7 PoolWorker-4
                  0 t8 PoolWorker-6
                  0 t9 PoolWorker-3
                  Should be 10 but is 0.
                  

                  <小時>

                  @dano 提供的當前最佳解決方案 - 我將自定義管理器與類代理混合在一起.


                  Current the best solution by @dano - I mixed custom manager with class proxy.

                  import multiprocessing
                  from multiprocessing.managers import BaseManager, NamespaceProxy
                  
                  
                  class Counter(object):
                    def __init__(self):
                      self.value = 0
                  
                    def update(self, value):
                      self.value += value
                  
                  
                  def update(counter_proxy, thread_id):
                    counter_proxy.update(1)
                  
                  class CounterManager(BaseManager):
                    pass
                  
                  class CounterProxy(NamespaceProxy):
                    _exposed_ = ('__getattribute__', '__setattr__', '__delattr__', 'update')
                  
                    def update(self, value):
                      callmethod = object.__getattribute__(self, '_callmethod')
                      return callmethod(self.update.__name__, (value,))
                  
                  CounterManager.register('Counter', Counter, CounterProxy)
                  
                  def main():
                    manager = CounterManager()
                    manager.start()
                  
                    counter = manager.Counter()
                    pool = multiprocessing.Pool(multiprocessing.cpu_count())
                    for i in range(10):
                      pool.apply(func = update, args = (counter, i))
                    pool.close()
                    pool.join()
                  
                    print 'Should be 10 but is %s.' % counter.value
                  
                  if __name__ == '__main__':
                    main()
                  

                  推薦答案

                  multiprocessing.Value 并非設計用于自定義類,它應該類似于 multiprocessing.sharedctypes.Value.相反,您需要創建一個 自定義管理器 并注冊您的課程用它.如果您不直接訪問 value ,而是通過方法修改/訪問它,您的生活也會更輕松,這些方法將由為您的類創建的默認 Proxy 導出默認.常規屬性(如 Counter.value)不是,因此如果沒有額外的自定義,它們就無法訪問.這是一個工作示例:

                  multiprocessing.Value isn't designed to be used with custom classes, it's supposed to be similar to a multiprocessing.sharedctypes.Value. Instead, you need to create a custom manager and register your class with it. Your life will also be easier if you don't access value directly, but modify/access it via methods, which will get exported by the default Proxy created for your class by default. Regular attributes (like Counter.value) aren't, so they aren't accessible without additional customization. Here's a working example:

                  import multiprocessing
                  from multiprocessing.managers import BaseManager
                  
                  class MyManager(BaseManager): pass
                  
                  def Manager():
                      m = MyManager()
                      m.start()
                      return m 
                  
                  class Counter(object):
                    def __init__(self):
                      self._value = 0
                  
                    def update(self, value):
                      self._value += value
                  
                    def get_value(self):
                        return self._value
                  
                  MyManager.register('Counter', Counter)
                  
                  def update(counter_proxy, thread_id):
                    counter_proxy.update(1)
                    print counter_proxy.get_value(), 't%s' % thread_id, 
                      multiprocessing.current_process().name
                    return counter_proxy
                  
                  def main():
                    manager = Manager()
                    counter = manager.Counter()
                    pool = multiprocessing.Pool(multiprocessing.cpu_count())
                    for i in range(10):
                      pool.apply(func=update, args=(counter, i))
                    pool.close()
                    pool.join()
                  
                    print 'Should be 10 but is %s.' % counter.get_value()
                  
                  if __name__ == '__main__':
                    main()
                  

                  輸出:

                  1 t0 PoolWorker-2
                  2 t1 PoolWorker-8
                  3 t2 PoolWorker-4
                  4 t3 PoolWorker-5
                  5 t4 PoolWorker-6
                  6 t5 PoolWorker-7
                  7 t6 PoolWorker-3
                  8 t7 PoolWorker-9
                  9 t8 PoolWorker-2
                  10 t9 PoolWorker-8
                  Should be 10 but is 10.
                  

                  這篇關于如何在進程之間共享一個類?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='Bbxb6'></tbody>
                • <legend id='Bbxb6'><style id='Bbxb6'><dir id='Bbxb6'><q id='Bbxb6'></q></dir></style></legend>
                • <small id='Bbxb6'></small><noframes id='Bbxb6'>

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

                            主站蜘蛛池模板: 日韩一区二区三区精品 | 亚洲精品一区在线观看 | 精品视频免费在线 | 69电影网 | 在线视频中文字幕 | 欧美精品1区2区3区 精品国产欧美一区二区 | 一区二区三区四区视频 | 成人av大全 | 久久久久久国产精品 | 亚洲一区二区在线播放 | 欧美激情综合色综合啪啪五月 | 一区二区在线不卡 | 久久精品一区二区三区四区 | 欧美日韩亚洲在线 | 亚洲一区影院 | 成人性视频在线 | 欧美一区二区小视频 | 午夜精品久久久久久久久久久久久 | 一区二区三区四区免费在线观看 | 国产精品久久国产精品 | 国产成人精品亚洲日本在线观看 | 免费观看一级特黄欧美大片 | 欧洲精品久久久久毛片完整版 | 三级黄视频在线观看 | caoporn免费在线视频 | 亚洲午夜小视频 | 在线观看不卡av | 天天夜干 | 亚洲欧洲一区二区 | 日韩欧美精品在线播放 | 日韩在线免费视频 | 超碰免费观看 | 久久福利网站 | 欧美亚洲视频 | 国产精品99久久久久久久久久久久 | 91极品视频 | 日韩色视频| 最新国产视频 | 国产精品一区二区欧美黑人喷潮水 | 欧美99久久精品乱码影视 | 伊人伊人|