我无法让pool.map或pool.starmap以我认为的方式工作

时间:2019-11-22 22:53:38

标签: parallel-processing python-multiprocessing

因此,对我而言,我无法确定应该使用pool.map()或pool.starmap()的哪种组合来完成此工作。下面的代码可以正确执行,依次触发两个runSim实例并附加结果。

results = []
argsToRun = []

for n in range(2):
    random.seed()
    env = simpy.Environment()  # Create the SimPy environment
    env.clockRate = 1e9
    argsToRun.append([env, adist, sdist, tdist, 108, intSpeed, runUntil])

for arg_list in argsToRun:
    line = runSim(*arg_list)
    results.append(line)

我的印象是,类似于以下代码的代码将是并行运行这些实例的简单方法(结果与顺序无关)……但它们似乎都不起作用。我肯定会错过明显的东西吗?

with multiprocessing.Pool() as pool:
    results = pool.map(runSim, argsToRun)
    pool.close()

没有。...

with multiprocessing.Pool() as pool:
    results = pool.starmap(runSim, argsToRun)
    pool.close()

1 个答案:

答案 0 :(得分:0)

经过大量的挫败和搜索,结果发现我的某些参数无法腌制。加上VSCode中的一个错误,该错误阻止了调试器的运行,这使诊断变得比原本应该的难得多。

切换到Pool()的pathos versions解决了此问题。

for n in range(15):
    random.seed()
    env = simpy.Environment()  # Create the SimPy environment
    argsToRun.append((env, adist, sdist, tdist, 108, intSpeed, runUntil))

with pathos.helpers.mp.Pool as pool:   ## tada!
    results = pool.starmap(runSim, argsToRun)