使用 python 多处理并行化 Monte Carlo 方法时出现问题
Issues when parallelize Monte Carlo method with python Multiprocessing
我想加速 Monte Carlo 方法,该方法基于 Multiprocessing 模块的 numpy 操作。我已阅读 enter link description here 并为某些任务编写了如下代码:
import func1, func2, func3, ... #some manipulations on the SAME numpy ndarray but each of then works independantly returns a independant result
import multiprocessing as mp
if __name__ == '__main__':
with mp.Pool(processes=mp.cpu_count()) as pool:
task1 = pool.Process(target=fun1, args(arg1, arg2, ...)
task2 = pool.Process(target=fun2, args(arg1, arg2, ...)
task3 = pool.Process(target=fun3, args(arg1, arg2, ...)
...
task1.start()
task2.start()
task3.start()
...
variable1 = task1.join() #In my case, I need to get the returns of these functions
variable2 = task2.join()
variable3 = task3.join()
...
喜欢大多数教程。但是我得到了
RuntimeError:An attempt has been made to start a new process before
the current process has finished its bootstrapping phase.
我真的是这个领域的新手,在我 post 提出这个问题之前苦苦挣扎了好几天。有人可以给我一些建议吗?
当我在 MacOS 上 运行 相同的程序时,我发现了这个 RunTimeError 问题。实际上,这个 RuntimeError 是由于 how Windows 编译 .py 文件的方式造成的。
要纠正这个问题,最简单的方法是将程序主体传递给函数 main()
(尽管在我的情况下,它很复杂)然后在 [=13] 中添加 freeze_support()
=] 模块。所以它最终看起来像这样:
import func1, func2, func3, ... #some manipulations on the SAME numpy ndarray 但每一个都独立工作 returns 一个独立的结果
import multiprocessing as mp
from multiprocessing import freeze_support()
def main():
with mp.Pool(processes=mp.cpu_count()) as pool:
task1 = pool.Process(target=fun1, args(arg1, arg2, ...)
task2 = pool.Process(target=fun2, args(arg1, arg2, ...)
task3 = pool.Process(target=fun3, args(arg1, arg2, ...)
...
task1.start()
task2.start()
task3.start()
...
variable1 = task1.join() #In my case, I need to get the returns of these functions
variable2 = task2.join()
variable3 = task3.join()
...
if __name__ == '__main__':
freeze_support()
main()
我想加速 Monte Carlo 方法,该方法基于 Multiprocessing 模块的 numpy 操作。我已阅读 enter link description here 并为某些任务编写了如下代码:
import func1, func2, func3, ... #some manipulations on the SAME numpy ndarray but each of then works independantly returns a independant result
import multiprocessing as mp
if __name__ == '__main__':
with mp.Pool(processes=mp.cpu_count()) as pool:
task1 = pool.Process(target=fun1, args(arg1, arg2, ...)
task2 = pool.Process(target=fun2, args(arg1, arg2, ...)
task3 = pool.Process(target=fun3, args(arg1, arg2, ...)
...
task1.start()
task2.start()
task3.start()
...
variable1 = task1.join() #In my case, I need to get the returns of these functions
variable2 = task2.join()
variable3 = task3.join()
...
喜欢大多数教程。但是我得到了
RuntimeError:An attempt has been made to start a new process before the current process has finished its bootstrapping phase.
我真的是这个领域的新手,在我 post 提出这个问题之前苦苦挣扎了好几天。有人可以给我一些建议吗?
当我在 MacOS 上 运行 相同的程序时,我发现了这个 RunTimeError 问题。实际上,这个 RuntimeError 是由于 how Windows 编译 .py 文件的方式造成的。
要纠正这个问题,最简单的方法是将程序主体传递给函数 main()
(尽管在我的情况下,它很复杂)然后在 [=13] 中添加 freeze_support()
=] 模块。所以它最终看起来像这样:
import func1, func2, func3, ... #some manipulations on the SAME numpy ndarray 但每一个都独立工作 returns 一个独立的结果
import multiprocessing as mp
from multiprocessing import freeze_support()
def main():
with mp.Pool(processes=mp.cpu_count()) as pool:
task1 = pool.Process(target=fun1, args(arg1, arg2, ...)
task2 = pool.Process(target=fun2, args(arg1, arg2, ...)
task3 = pool.Process(target=fun3, args(arg1, arg2, ...)
...
task1.start()
task2.start()
task3.start()
...
variable1 = task1.join() #In my case, I need to get the returns of these functions
variable2 = task2.join()
variable3 = task3.join()
...
if __name__ == '__main__':
freeze_support()
main()