平行。为了在Python中

2024-07-04 07:51:09 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有类似C的优秀平行。为了在Python中?我只想做一个计算

[simu(c) for c in clusterSizes]

平行的。最简单的存档方法是什么?在

PS:我试过joblib,但在我的例子中,它只是启动、启动和启动进程,直到我必须重新启动我的机器。在


Tags: 方法in机器for进程例子pssimu
3条回答

在python3中,concurrent.futures(在标准库中)中有一个并行映射。我认为它甚至被作为Python2.7的一个模块进行了后端口移植。 编辑http://pypi.python.org/pypi/futures

正如另一个答案中提到的,线程对您没有帮助。相反,您必须使用多个进程。在

编辑从文档中可以很简单地看到:

with concurrent.futures.ProcessPoolExecutor() as executor:
    for result in executor.map(simu, clusterSizes)
        pass # save result

joblib基于multiprocessing,并且具有与@EwyynTomato在注释中指出的相同的problem on Windows:脚本的主入口点必须受到__name__ == "__main__"检查的保护。在

from joblib import Parallel, delayed

if __name__ == "__main__":
    result = Parallel(n_jobs=-1)(delayed(simu)(c) for c in clusterSizes)
    # process result

multiprocessing包中,可以创建Pool对象,并使用map方法:

http://docs.python.org/library/multiprocessing.html

与joblib一样,您必须使用__name__ == "__main__"来保护主入口点,否则您将创建一个分叉炸弹(在导入脚本时需要重新启动计算机)。在

要映射的函数必须是可选择的。在

相关问题 更多 >

    热门问题