单个并行运行的每个“并行”结果都需要写入其自己的文件中。如果我能够说出每个结果的名称,这个问题也可以得到解决。
我有一个生成一些数据的函数。每次运行时,数据都会略有不同,因此我需要运行几次。我目前有使用 joblib.Parallel 来加速此过程的工作代码。问题在于,结果是所有并行运行的一长串列表,将其写入单独的文件非常复杂且容易出错。
def fn(x):
for i in np.linspace(0, x, 1000):
a = x
b = 2*x
return a, b
ans = Parallel(n_jobs=-1)(delayed(fn)(x) for x in np.linspace(0,5,5))
ans
# I need to either name/extract each result in the list below, or directly write each into its own file
out[]: [(0.0, 0.0), (1.25, 2.5), (2.5, 5.0), (3.75, 7.5), (5.0, 10.0)]
最佳答案
如果您只想让每个进程写入它自己的文件,您可以执行以下操作。
def fn(x):
for i in np.linspace(0, x, 1000):
a = x
b = 2*x
with open(str(x)+"_file.csv", 'w') as file:
file.write(a, b)
return a, b
ans = Parallel(n_jobs=-1)(delayed(fn)(x) for x in np.linspace(0,5,5))
但我不确定您为什么要这样做,如果您更详细地让我们知道您的最终目标是什么,我相信我们可以提供更多帮助。
关于python - 有没有办法同时将每个 joblib.parallel 运行的结果写入自己的文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55522652/