多处理 - 无法将列表写入 csv(TypeError:'ApplyResult' 对象不可迭代)

Has*_*hmi 4 python multiprocessing python-3.x

我有一个用于分析大型 csv 文件的脚本,所以我需要启动多处理。我使用了multiprocessing模块并且它工作正常但是如果我尝试将输出列表写入 csv 文件,它会引发错误:

TypeError: 'ApplyResult' object is not iterable
Run Code Online (Sandbox Code Playgroud)

我查找了一些解决方案,包括多处理:TypeError: 'int' object is not iterable but could not solve the problem。这是代码:

from multiprocessing import Pool
import csv
pool = Pool()

nodes = [['1001', '2008-01-06T02:12:13Z', ['']],
        ['1002', '2008-01-06T02:13:55Z', ['']],
        ['1003', '2008-01-06T02:13:00Z',  ['Lion', 'Rhinoceros', 'Leopard', 'Panda']],
        ['1004', '2008-01-06T02:15:20Z', ['Lion', 'Leopard', 'Eagle', 'Panda', 'Tiger']],
        ['1005', '2008-01-06T02:15:48Z', ['Lion', 'Panda', 'Cheetah', 'Goat', 'Tiger']],
        ['1006', '2008-01-06T02:13:30Z', ['']],
        ['1007', '2008-01-06T02:13:38Z', ['Cheetah', 'Tiger', 'Goat']]]

def nodes_to_links(nodes_list):
    output_list = []
    for ii, elem in enumerate(nodes_list[:-1]):
        for jj in range(ii + 1, len(nodes_list)):
            common = set(elem[-1]).intersection(
                set(nodes_list[jj][-1]))
            if len(common) > 0 and common != {''}:
                output_list.append([elem[0], nodes_list[jj][0], ','.join(map(str, list(common)))])
    return output_list

#links = nodes_to_links(nodes) ###This works perfect without multiprocessing
links = pool.apply_async(nodes_to_links, (nodes))  ### This works if I don't write the list "links" to a csv, but not otherwise

# Write links to a csv file
def writeCSV(list_to_write, OutputFileName):
    file = open(OutputFileName, 'w', newline='')
    writer = csv.writer(file, quotechar='"', delimiter=';',quoting=csv.QUOTE_ALL, skipinitialspace=True)
    for row in list_to_write:
        writer.writerow(row)

writeCSV(links,'output_files/test.csv')
Run Code Online (Sandbox Code Playgroud)

我尝试使用pool.join()pool.close()但这也无济于事。任何人都可以请帮忙。需要注意的是,这个列表nodes非常大,我只是在这里举个例子,所以多处理是必不可少的。如果我的 CPU 中有四个内核和 8 个线程,我还能知道如何指定处理器数量吗?

mat*_*tch 5

pool.apply_async返回一个pool.AsyncResult对象。这个对象是不可迭代的——相反,你需要调用它的get()方法——即writeCSV(links.get(),'output_files/test.csv')(带有可选的超时)。

  • 我实际上并没有运行它 - 只是在看 mp 文档。看起来您还有其他 2 个错误 - 首先,`pool = Pool()` 必须在定义了 `nodes` 和 `nodes_to_links` 之后出现,其次,`apply_async` 的参数是一个元组,所以你的意思是 `pool.apply_async (nodes_to_links, (nodes,))`。这些变化似乎对我有用。 (2认同)