我有一个多处理代码,每个进程都必须对相同的数据进行不同的分析。
我已执行:
with concurrent.futures.ProcessPoolExecutor() as executor:
res = executor.map(goal_fcn, p, [global_DataFrame], [global_String])
for f in concurrent.futures.as_completed(res):
fp = res和职能:
def goal_fcn(x, DataFrame, String):
return heavy_calculation(x, DataFrame, String)问题是goal_fcn只被调用一次,而应该是多次调用。
在调试器中,我现在检查了变量p,它有多个列和多个行。在goal_fcn内部,变量x只有第一行--看起来不错。
但是这个函数只调用一次。没有错误,代码只执行接下来的步骤。
即使我修改了变量p = [1,3,4,5],当然还有代码。goal_fcn只执行一次
我必须使用map(),因为需要保持输入和输出之间的顺序。
发布于 2022-02-11 13:24:09
map的工作方式类似于zip。它一次终止至少一个输入序列在它的末端。您的[global_DataFrame]和[global_String]列表每个都有一个元素,因此这是映射结束的地方。
有两种方法可以解决这个问题:
itertools.product。这相当于“所有数据帧、所有字符串、所有p”的运行。就像这样:def goal_fcn(x_DataFrame_String):
x, DataFrame, String = x_DataFrame_String
...
executor.map(goal_fcn, itertools.product(p, [global_DataFrame], [global_String]))def goal_fcn(x, DataFrame, String):
pass
bound = functools.partial(goal_fcn, DataFrame=global_DataFrame, String=global_String)
executor.map(bound, p)https://stackoverflow.com/questions/71080033
复制相似问题