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

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

      <legend id='pSjRQ'><style id='pSjRQ'><dir id='pSjRQ'><q id='pSjRQ'></q></dir></style></legend>
      • <bdo id='pSjRQ'></bdo><ul id='pSjRQ'></ul>

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

        <tfoot id='pSjRQ'></tfoot>

      1. 具有多處理功能的 Python 裝飾器失敗

        Python decorator with multiprocessing fails(具有多處理功能的 Python 裝飾器失敗)
              • <bdo id='DoHt0'></bdo><ul id='DoHt0'></ul>

                <tfoot id='DoHt0'></tfoot>

                  <tbody id='DoHt0'></tbody>

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

                  <legend id='DoHt0'><style id='DoHt0'><dir id='DoHt0'><q id='DoHt0'></q></dir></style></legend>
                  <i id='DoHt0'><tr id='DoHt0'><dt id='DoHt0'><q id='DoHt0'><span id='DoHt0'><b id='DoHt0'><form id='DoHt0'><ins id='DoHt0'></ins><ul id='DoHt0'></ul><sub id='DoHt0'></sub></form><legend id='DoHt0'></legend><bdo id='DoHt0'><pre id='DoHt0'><center id='DoHt0'></center></pre></bdo></b><th id='DoHt0'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='DoHt0'><tfoot id='DoHt0'></tfoot><dl id='DoHt0'><fieldset id='DoHt0'></fieldset></dl></div>
                  本文介紹了具有多處理功能的 Python 裝飾器失敗的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

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

                  我想在隨后將傳遞給多處理池的函數(shù)上使用裝飾器.但是,代碼因PicklingError: Can't pickle : attribute lookup __builtin__.function failed"而失敗.我不太明白為什么它在這里失敗.我確信這很簡(jiǎn)單,但我找不到.下面是一個(gè)最小的工作"示例.我認(rèn)為使用 functools 函數(shù)就足以讓它工作.

                  I would like to use a decorator on a function that I will subsequently pass to a multiprocessing pool. However, the code fails with "PicklingError: Can't pickle : attribute lookup __builtin__.function failed". I don't quite see why it fails here. I feel certain that it's something simple, but I can't find it. Below is a minimal "working" example. I thought that using the functools function would be enough to let this work.

                  如果我注釋掉函數(shù)裝飾,它可以正常工作.我在這里誤解的 multiprocessing 是什么?有什么辦法可以做到嗎?

                  If I comment out the function decoration, it works without an issue. What is it about multiprocessing that I'm misunderstanding here? Is there any way to make this work?

                  編輯:在添加了一個(gè)可調(diào)用的類裝飾器一個(gè)函數(shù)裝飾器之后,函數(shù)裝飾器按預(yù)期工作.可調(diào)用的類裝飾器繼續(xù)失敗.防止它被腌制的可調(diào)用類版本是什么?

                  Edit: After adding both a callable class decorator and a function decorator, it turns out that the function decorator works as expected. The callable class decorator continues to fail. What is it about the callable class version that keeps it from being pickled?

                  import random
                  import multiprocessing
                  import functools
                  
                  class my_decorator_class(object):
                      def __init__(self, target):
                          self.target = target
                          try:
                              functools.update_wrapper(self, target)
                          except:
                              pass
                  
                      def __call__(self, elements):
                          f = []
                          for element in elements:
                              f.append(self.target([element])[0])
                          return f
                  
                  def my_decorator_function(target):
                      @functools.wraps(target)
                      def inner(elements):
                          f = []
                          for element in elements:
                              f.append(target([element])[0])
                          return f
                      return inner
                  
                  @my_decorator_function
                  def my_func(elements):
                      f = []
                      for element in elements:
                          f.append(sum(element))
                      return f
                  
                  if __name__ == '__main__':
                      elements = [[random.randint(0, 9) for _ in range(5)] for _ in range(10)]
                      pool = multiprocessing.Pool(processes=4)
                      results = [pool.apply_async(my_func, ([e],)) for e in elements]
                      pool.close()
                      f = [r.get()[0] for r in results]
                      print(f)
                  

                  推薦答案

                  問(wèn)題是 pickle 需要有一些方法來(lái)重新組裝你 pickle 的所有東西.請(qǐng)參閱此處了解可以腌制的內(nèi)容:

                  The problem is that pickle needs to have some way to reassemble everything that you pickle. See here for a list of what can be pickled:

                  http://docs.python.org/library/pickle.html#what-c??an-be-pickled-and-unpickled

                  酸洗my_func時(shí),需要酸洗以下組件:

                  When pickling my_func, the following components need to be pickled:

                  • my_decorator_class 的一個(gè)實(shí)例,稱為 my_func.

                  • An instance of my_decorator_class, called my_func.

                  這很好.Pickle 將存儲(chǔ)類的名稱并腌制其 __dict__ 內(nèi)容.unpickling 時(shí),它使用名稱查找類,然后創(chuàng)建一個(gè)實(shí)例并填寫(xiě) __dict__ 內(nèi)容.但是,__dict__ 內(nèi)容存在問(wèn)題...

                  This is fine. Pickle will store the name of the class and pickle its __dict__ contents. When unpickling, it uses the name to find the class, then creates an instance and fills in the __dict__ contents. However, the __dict__ contents present a problem...

                  存儲(chǔ)在 my_func.target 中的原始 my_func 的實(shí)例.

                  The instance of the original my_func that's stored in my_func.target.

                  這不太好.它是頂層的函數(shù),通常可以腌制.Pickle 將存儲(chǔ)函數(shù)的名稱.然而,問(wèn)題在于名稱my_func"不再綁定到未修飾的函數(shù),而是綁定到修飾的函數(shù).這意味著 pickle 將無(wú)法查找未修飾的函數(shù)來(lái)重新創(chuàng)建對(duì)象.遺憾的是,pickle 無(wú)法知道它試圖腌制的對(duì)象總是可以在名稱 __main__.my_func 下找到.

                  This isn't so good. It's a function at the top-level, and normally these can be pickled. Pickle will store the name of the function. The problem, however, is that the name "my_func" is no longer bound to the undecorated function, it's bound to the decorated function. This means that pickle won't be able to look up the undecorated function to recreate the object. Sadly, pickle doesn't have any way to know that object it's trying to pickle can always be found under the name __main__.my_func.

                  你可以這樣改變它,它會(huì)起作用:

                  You can change it like this and it will work:

                  import random
                  import multiprocessing
                  import functools
                  
                  class my_decorator(object):
                      def __init__(self, target):
                          self.target = target
                          try:
                              functools.update_wrapper(self, target)
                          except:
                              pass
                  
                      def __call__(self, candidates, args):
                          f = []
                          for candidate in candidates:
                              f.append(self.target([candidate], args)[0])
                          return f
                  
                  def old_my_func(candidates, args):
                      f = []
                      for c in candidates:
                          f.append(sum(c))
                      return f
                  
                  my_func = my_decorator(old_my_func)
                  
                  if __name__ == '__main__':
                      candidates = [[random.randint(0, 9) for _ in range(5)] for _ in range(10)]
                      pool = multiprocessing.Pool(processes=4)
                      results = [pool.apply_async(my_func, ([c], {})) for c in candidates]
                      pool.close()
                      f = [r.get()[0] for r in results]
                      print(f)
                  


                  您已經(jīng)觀察到裝飾器功能在類不起作用時(shí)起作用.我相信這是因?yàn)?functools.wraps 修改了裝飾函數(shù),使其具有它包裝的函數(shù)的名稱和其他屬性.就 pickle 模塊而言,它與普通的頂級(jí)函數(shù)沒(méi)有區(qū)別,因此它通過(guò)存儲(chǔ)其名稱來(lái)腌制它.解壓后,名稱將綁定到裝飾函數(shù),因此一切正常.


                  You have observed that the decorator function works when the class does not. I believe this is because functools.wraps modifies the decorated function so that it has the name and other properties of the function it wraps. As far as the pickle module can tell, it is indistinguishable from a normal top-level function, so it pickles it by storing its name. Upon unpickling, the name is bound to the decorated function so everything works out.

                  這篇關(guān)于具有多處理功能的 Python 裝飾器失敗的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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屋-程序員軟件開(kāi)
                  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ì)象沒(méi)有屬性“f)
                      <tbody id='oxlZo'></tbody>
                    <tfoot id='oxlZo'></tfoot>
                      <bdo id='oxlZo'></bdo><ul id='oxlZo'></ul>
                      <legend id='oxlZo'><style id='oxlZo'><dir id='oxlZo'><q id='oxlZo'></q></dir></style></legend>
                    • <i id='oxlZo'><tr id='oxlZo'><dt id='oxlZo'><q id='oxlZo'><span id='oxlZo'><b id='oxlZo'><form id='oxlZo'><ins id='oxlZo'></ins><ul id='oxlZo'></ul><sub id='oxlZo'></sub></form><legend id='oxlZo'></legend><bdo id='oxlZo'><pre id='oxlZo'><center id='oxlZo'></center></pre></bdo></b><th id='oxlZo'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='oxlZo'><tfoot id='oxlZo'></tfoot><dl id='oxlZo'><fieldset id='oxlZo'></fieldset></dl></div>

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

                            主站蜘蛛池模板: 欧美一级免费 | 亚洲福利一区二区 | 免费一级欧美在线观看视频 | 精品欧美乱码久久久久久 | 不卡一区二区三区四区 | 亚洲女人天堂成人av在线 | 久久国产日本 | 偷拍自拍网| 久久综合入口 | 精品日韩一区 | 欧美日韩不卡在线 | 久久成人精品一区二区三区 | 影音先锋成人资源 | 免费视频一区 | 91精品国产色综合久久不卡蜜臀 | 一级毛片成人免费看a | 久久久久亚洲精品 | 精品香蕉一区二区三区 | 国产高清久久 | 亚洲精品区 | 日本一二区视频 | 精品一二区 | 青青草一区 | 中文字幕一区二区三区日韩精品 | 欧美日本在线观看 | 九九久久国产精品 | 日中文字幕在线 | 成av在线 | 国产三级电影网站 | 久久久欧洲 | 亚洲国产成人在线视频 | 亚洲一区在线免费观看 | 免费看黄色片 | 国产欧美一区二区三区在线播放 | 五月天婷婷综合 | 伊人狠狠干 | av一区二区三区四区 | 欧产日产国产精品国产 | 久久久久久国产 | 精品美女 | 国产福利一区二区 |