Kann ich multiprocessing.Pool in einer Methode einer Klasse verwenden?

Ich versuche @ zu benutzmultiprocessing in meinem Code für eine bessere Leistung.

Ich habe jedoch folgende Fehlermeldung erhalten:

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

Ich habe es anders versucht und habe diesen Fehler bekommen:

TypeError: cannot serialize '_io.TextIOWrapper' object

Mein Code sieht so aus:

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)

Ich denke, dass der Fehler ausgelöst wird, weil das @ nicht verwendet wiPool in der Hauptfunktion.

Ist meine Vermutung richtig? Und wie kann ich meinen Code ändern, um den Fehler zu beheben?

Antworten auf die Frage(1)

Ihre Antwort auf die Frage