Python 使用“多处理”模块并行枚举循环
我的代码如下所示:Python 使用“多处理”模块并行枚举循环,python,python-3.x,multiprocessing,Python,Python 3.x,Multiprocessing,我的代码如下所示: # Features' construction - Multiprocessing # import pandas as pd import time import multiprocessing def features_construct(index, row): dict_features ={} ... return(dict_features) data = [] for index, row in enumerate(ra
# Features' construction - Multiprocessing #
import pandas as pd
import time
import multiprocessing
def features_construct(index, row):
dict_features ={}
...
return(dict_features)
data = []
for index, row in enumerate(raw_data):
data.append(features_construct(index, row))
df_data = pd.DataFrame.from_records(data)
raw_data
是一个包含两个元素列表的列表,其中每个元素都是json
如何将此代码与python的多处理模块并行?运行:
import multiprocessing
output=[]
raw_data = [[2,2],[4,3],[5,1],[6,3],[7,2],[12,4],[12,2]]
def features_construct(index, row):
dict_features ={}
...
return(dict_features)
def combinations():
pool = multiprocessing.Pool(64)
outputs=pool.starmap(features_construct, raw_data)
### check starmap above
return outputs
if __name__ == '__main__':
output.append(combinations())
谢谢你的回答。但是,我收到了这个错误:
TypeError:features\u construct()缺少一个必需的位置参数:“row”
,这是很合理的,因为我看不到您在哪里传递features\u construct
@Poete的参数,请为函数def features\u construct(索引,行)添加完整的代码:
。我的完整代码非常长,您不需要理解如何编写多处理代码。如果您知道我在功能构造
代码中使用了索引和元素(来自枚举
),就足够了。正如我在上面的评论中写到的,我没有在feature\u construct
的代码中得到错误,但就在这之前,因为我没有简单地将所需的参数传递给函数;)固定的现在检查@PoeteThat更好,但是现在我在我的第一个特性(以及在第一次迭代中)的代码中得到一个错误:`File',第54行,在features\u construct dict\u features['feature\u 1{}.format(I+1)]=len中(row[I]['text'])键错误:0。只是为了说明我没有收到任何错误,而且在我的原始代码中肯定没有出现
keyrorm`。据我所知,当程序将row[I]
与row[0]
子分区时会发生错误,但它没有发现这一点,但根据我的原始代码,这是不可能的。所以我不确定星图是否符合我们的要求。