【发布时间】:2021-04-08 04:26:09
【问题描述】:
我试图将 for 循环转换为 multiprocessing.Pool().map 函数。在这里,我创建了一个空的csr_matrix 并基于索引并行分配值。但这没有按预期工作。执行代码需要几分钟,但byte_bigram_matrix 仍然为空。
byte_bigram_matrix = csr_matrix((10868,66049))
def calculate_bigram(file):
with open('byteFiles/'+file,"r") as byte_file:
byte_bigram_matrix[files.index(file)] = csr_matrix(#someprocessing to calculate bigrams)
from multiprocessing import Pool
#Using multiprocessing to calculate bi-grams
files = os.listdir('filesPath/')
p = Pool() #Using max cores as processors
p.map(calculate_bigram, files)
p.close()
p.join()
问题:
我们不能使用 Multiprocessing 中的 map 函数并行索引 N 维数组/矩阵的值吗?或者如何使用多处理来完成这项任务?
【问题讨论】:
标签: python arrays multithreading multidimensional-array multiprocessing