python - 多处理:将值并行分配给 N 维数组/矩阵
问题描述
我试图将 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()
问题:
我们不能使用map
Multiprocessing 中的函数并行索引 ND 数组/矩阵的值吗?或者如何使用多处理来完成这项任务?
解决方案
首先文件是“filePath /”中文件名称的一维python列表,据
我所知问题在于calculate_bigram,因为您使用读取而不是写入打开文件,因此尝试时会出错写给它。我试过这个:
def calculate_bigram(file):
if os.path.isfile(file):
with open(file, "w") as byte_file:
byte_file.write("this is a test")
import os
from multiprocessing import Pool
if __name__ == "__main__":
#Using multiprocessing to calculate bi-grams
files = os.listdir('files/')
path = os.path.dirname(__file__)
for idx, file in enumerate(files):
files[idx] = os.path.join(path, "files", file)
with Pool(processes=4) as pool:
pool.map(calculate_bigram, files)
文件目录看起来像这样
files
|-> a.txt
|-> b.txt
|-> sub
|-> c.txt
另外,您必须提供完整路径,而不是与您执行的文件相关的路径,因此
path = os.path.dirname(__file__)
for idx, file in enumerate(files):
files[idx] = os.path.join(path, "files", file)
因为池更改了执行目录,所以文件最终会出现你不想要的东西
编辑:您的评论:\您仍然必须指定完整路径,而不是与当前执行相关的路径。至少这对我来说是这样的
推荐阅读
- angular - 调用多个 API 并按顺序订阅它们
- angular-validation - 在不使用多个 *ngIf 的情况下以角度反应形式显示错误消息的更好和更有效的方法
- java - Java - equals() 和 hashCode() 实现
- netsuite - 如何使用 Suitescript 2.0 在 netsuite 中设置 RESTlet 的日期
- active-directory - 使用 ldapsearch 查询 Samba AD 服务器失败并出现 ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)
- php - 如何访问对象属性(arraycollection)?
- python - 通过 ODBC 修改现有的 MS Access 查询
- c++ - 在 lambda 中包装并执行 packaged_task
- three.js - 填充 Vector3 数组
- ruby-on-rails - Rails Button_tag 不提交编辑表单,但提交新表单