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

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

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

      <tfoot id='cla8v'></tfoot>

        我可以在類的方法中使用 multiprocessing.Pool 嗎?

        Can I use multiprocessing.Pool in a method of a class?(我可以在類的方法中使用 multiprocessing.Pool 嗎?)

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

                  <tbody id='VVolC'></tbody>
              • <tfoot id='VVolC'></tfoot>

              • <small id='VVolC'></small><noframes id='VVolC'>

                <legend id='VVolC'><style id='VVolC'><dir id='VVolC'><q id='VVolC'></q></dir></style></legend>
                  本文介紹了我可以在類的方法中使用 multiprocessing.Pool 嗎?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

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

                  我正在嘗試在我的代碼中使用 multiprocessing 以獲得更好的性能.

                  I am tring to use multiprocessing in my code for better performance.

                  但是,我收到如下錯(cuò)誤:

                  However, I got an error as follows:

                  Traceback (most recent call last):
                    File "D:EpubBuilderTinyEpub.py", line 49, in <module>
                      e.epub2txt()
                    File "D:EpubBuilderTinyEpub.py", line 43, in epub2txt
                      tempread = self.get_text()
                    File "D:EpubBuilderTinyEpub.py", line 29, in get_text
                      txtlist = pool.map(self.char2text,charlist)
                    File "C:Python34libmultiprocessingpool.py", line 260, in map
                      return self._map_async(func, iterable, mapstar, chunksize).get()
                    File "C:Python34libmultiprocessingpool.py", line 599, in get
                      raise self._value
                    File "C:Python34libmultiprocessingpool.py", line 383, in _handle_tasks
                      put(task)
                    File "C:Python34libmultiprocessingconnection.py", line 206, in send
                      self._send_bytes(ForkingPickler.dumps(obj))
                    File "C:Python34libmultiprocessing
                  eduction.py", line 50, in dumps
                      cls(buf, protocol).dump(obj)
                  TypeError: cannot serialize '_io.BufferedReader' object
                  

                  我嘗試了另一種方法并得到了這個(gè)錯(cuò)誤:

                  I have tried it an other way and got this error:

                  TypeError: cannot serialize '_io.TextIOWrapper' object
                  

                  我的代碼如下所示:

                  from multiprocessing import Pool
                  class Book(object):
                      def __init__(self, arg):
                          self.namelist = arg
                      def format_char(self,char):
                          char = char + "a"
                          return char
                      def format_book(self):
                          self.tempread = ""
                          charlist = [f.read() for f in self.namelist] #list of char
                          with Pool() as pool:
                              txtlist = pool.map(self.format_char,charlist)
                          self.tempread = "".join(txtlist)
                          return self.tempread
                  
                  if __name__ == '__main__':
                      import os
                      b = Book([open(f) for f in os.listdir()])
                      t = b.format_book()
                      print(t)
                  

                  我認(rèn)為這個(gè)錯(cuò)誤是因?yàn)闆]有在main函數(shù)中使用Pool引起的.

                  I think that the error is raised because of not using the Pool in the main function.

                  我的猜想對(duì)嗎?以及如何修改我的代碼來修復(fù)錯(cuò)誤?

                  Is my conjecture right? And how can I modify my code to fix the error?

                  推薦答案

                  問題是你在 Book 實(shí)例中有一個(gè)不可選擇的實(shí)例變量 (namelist).因?yàn)槟趯?shí)例方法上調(diào)用 pool.map,并且您在 Windows 上運(yùn)行,所以整個(gè)實(shí)例需要是可挑選的,以便將其傳遞給子進(jìn)程.Book.namelist 是一個(gè)打開的文件對(duì)象(_io.BufferedReader),不能被pickle.您可以通過多種方式解決此問題.根據(jù)示例代碼,您可以將 format_char 設(shè)為頂級(jí)函數(shù):

                  The issue is that you've got an unpicklable instance variable (namelist) in the Book instance. Because you're calling pool.map on an instance method, and you're running on Windows, the entire instance needs to be picklable in order for it to be passed to the child process. Book.namelist is a open file object (_io.BufferedReader), which can't be pickled. You can fix this a couple of ways. Based on the example code, it looks like you could just make format_char a top-level function:

                  def format_char(char):
                      char = char + "a"
                      return char
                  
                  
                  class Book(object):
                      def __init__(self, arg):
                          self.namelist = arg
                  
                      def format_book(self):
                          self.tempread = ""
                          charlist = [f.read() for f in self.namelist] #list of char
                          with Pool() as pool:
                              txtlist = pool.map(format_char,charlist)
                          self.tempread = "".join(txtlist)
                          return self.tempread
                  

                  但是,如果實(shí)際上,您需要 format_char 作為實(shí)例方法,則可以使用 __getstate__/__setstate__ 通過刪除 使 Book 可挑選namelist 在腌制之前從實(shí)例中獲取參數(shù):

                  However, if in reality, you need format_char to be an instance method, you can use __getstate__/__setstate__ to make Book picklable, by removing the namelist argument from the instance before pickling it:

                  class Book(object):
                      def __init__(self, arg):
                          self.namelist = arg
                  
                      def __getstate__(self):
                          """ This is called before pickling. """
                          state = self.__dict__.copy()
                          del state['namelist']
                          return state
                  
                      def __setstate__(self, state):
                          """ This is called while unpickling. """
                          self.__dict__.update(state)
                  
                      def format_char(self,char):
                          char = char + "a"
                  
                      def format_book(self):
                          self.tempread = ""
                          charlist = [f.read() for f in self.namelist] #list of char
                          with Pool() as pool:
                              txtlist = pool.map(self.format_char,charlist)
                          self.tempread = "".join(txtlist)
                          return self.tempread
                  

                  只要你不需要在子進(jìn)程中訪問namelist就可以了.

                  This would be ok as long as you don't need to access namelist in the child process.

                  這篇關(guān)于我可以在類的方法中使用 multiprocessing.Pool 嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(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屋-程序員軟件開
                  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ì)象沒有屬性“f)
                    <bdo id='2G30n'></bdo><ul id='2G30n'></ul>
                      <tbody id='2G30n'></tbody>

                        <tfoot id='2G30n'></tfoot>

                        1. <small id='2G30n'></small><noframes id='2G30n'>

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

                          • 主站蜘蛛池模板: 午夜视频网 | 成人免费视频网 | 99久久99| 国产91丝袜在线播放 | 亚洲精品888 | 91大片| 69精品久久久久久 | 影音av | 久久久国产一区二区 | 巨大黑人极品videos精品 | 欧美一区二区三区在线观看视频 | 婷婷成人在线 | 亚洲国产片 | 成人精品鲁一区一区二区 | 精精国产xxxx视频在线播放7 | 亚洲天天干 | 99久久婷婷 | 久久大 | 亚洲国产一区二区在线 | 91精品国产综合久久久久久丝袜 | 精品日韩 | 午夜视频一区 | 男女啪啪高潮无遮挡免费动态 | 拍真实国产伦偷精品 | 午夜爽爽爽男女免费观看 | 亚洲欧美激情精品一区二区 | 国产一级一级毛片 | www国产成人免费观看视频 | 成人在线观看免费视频 | 视频一区二区三区中文字幕 | 中文字幕第二区 | 国产视频一区在线观看 | 精品国产99 | 日韩精品视频在线播放 | 亚洲精品国产成人 | 日本三级电影在线看 | 欧美久久一级特黄毛片 | 欧美a∨ | 久久国产一区二区三区 | 国产午夜精品视频 | 北条麻妃av一区二区三区 |