类型错误:“MapResult”对象不可使用 pathos.multiprocessing 迭代

Sam*_*Sam 5 python multiprocessing python-3.x python-multiprocessing pathos

我正在对我拥有的数据集运行拼写校正功能。我曾经from pathos.multiprocessing import ProcessingPool as Pool做过这项工作。处理完成后,我想实际访问结果。这是我的代码:

import codecs
import nltk

from textblob import TextBlob
from nltk.tokenize import sent_tokenize
from pathos.multiprocessing import ProcessingPool as Pool

class SpellCorrect():

    def load_data(self, path_1):
        with codecs.open(path_1, "r", "utf-8") as file:
            data = file.read()
        return sent_tokenize(data)

    def correct_spelling(self, data):
        data = TextBlob(data)
        return str(data.correct())

    def run_clean(self, path_1):
        pool = Pool()
        data = self.load_data(path_1)
        return pool.amap(self.correct_spelling, data)

if __name__ == "__main__":
    path_1 = "../Data/training_data/training_corpus.txt"
    SpellCorrect = SpellCorrect()
    result = SpellCorrect.run_clean(path_1)
    print(result)
    result = " ".join(temp for temp in result)
    with codecs.open("../Data/training_data/training_data_spell_corrected.txt", "a", "utf-8") as file:
        file.write(result)
Run Code Online (Sandbox Code Playgroud)

如果您查看主块,当我查看时,我会print(result)得到一个类型为 的对象<multiprocess.pool.MapResult object at 0x1a25519f28>

我尝试使用 访问结果result = " ".join(temp for temp in result),但随后出现以下错误TypeError: 'MapResult' object is not iterable。我试过将它类型转换为 list list(result),但仍然是同样的错误。我能做些什么来解决这个问题?

Ren*_* B. 5

multiprocess.pool.MapResult对象不可迭代,因为它继承自AsyncResult并且具有以下方法:

  • wait([timeout]) 等待,直到结果可用或直到超时秒数过去。此方法始终返回 None。

  • read()返回调用是否完成。

  • success()返回调用是否完成且未引发异常。如果结果未准备好,将引发 AssertionError。

  • get([timeout])到达时返回结果。如果 timeout 不是 None 并且结果没有在 timeout 秒内到达,则引发 TimeoutError 。如果远程调用引发了异常,则该异常将通过 get() 重新引发为 RemoteError。

您可以在此处查看如何使用 get() 函数的示例: https://docs.python.org/2/library/multiprocessing.html#using-a-pool-of-workers

from multiprocessing import Pool, TimeoutError
import time
import os

def f(x):
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)              # start 4 worker processes

    # print "[0, 1, 4,..., 81]"
    print pool.map(f, range(10))

    # print same numbers in arbitrary order
    for i in pool.imap_unordered(f, range(10)):
        print i

    # evaluate "f(20)" asynchronously
    res = pool.apply_async(f, (20,))      # runs in *only* one process
    print res.get(timeout=1)              # prints "400"

    # evaluate "os.getpid()" asynchronously
    res = pool.apply_async(os.getpid, ()) # runs in *only* one process
    print res.get(timeout=1)              # prints the PID of that process

    # launching multiple evaluations asynchronously *may* use more processes
    multiple_results = [pool.apply_async(os.getpid, ()) for i in range(4)]
    print [res.get(timeout=1) for res in multiple_results]

    # make a single worker sleep for 10 secs
    res = pool.apply_async(time.sleep, (10,))
    try:
        print res.get(timeout=1)
    except TimeoutError:
        print "We lacked patience and got a multiprocessing.TimeoutError"
Run Code Online (Sandbox Code Playgroud)