gpt4 book ai didi

python - 运行时错误 :freeze_support() on Mac

转载 作者:行者123 更新时间:2023-12-03 20:26:17 25 4
gpt4 key购买 nike

我是python新手。我想学习如何在 python 中进行并行处理。我看到了下面的例子:

import multiprocessing as mp

np.random.RandomState(100)
arr = np.random.randint(0, 10, size=[20, 5])
data = arr.tolist()

def howmany_within_range_rowonly(row, minimum=4, maximum=8):
count = 0
for n in row:
if minimum <= n <= maximum:
count = count + 1
return count

pool = mp.Pool(mp.cpu_count())

results = pool.map(howmany_within_range_rowonly, [row for row in data])

pool.close()

print(results[:10])

但是当我运行它时,发生了这个错误:
RuntimeError: 
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.

This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:

if __name__ == '__main__':
freeze_support()
...

The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.

我该怎么办?

最佳答案

如果您将所有内容都放在这个 if __name__ == "__main__" 内的全局范围内如下 block ,您应该会发现您的程序的行为与您预期的一样:

def howmany_within_range_rowonly(row, minimum=4, maximum=8):
count = 0
for n in row:
if minimum <= n <= maximum:
count = count + 1
return count

if __name__ == "__main__":
np.random.RandomState(100)
arr = np.random.randint(0, 10, size=[20, 5])
data = arr.tolist()
pool = mp.Pool(mp.cpu_count())

results = pool.map(howmany_within_range_rowonly, [row for row in data])

pool.close()

print(results[:10])

如果没有这种保护,如果您当前的模块是从不同的模块导入的,您的多处理代码将被执行。这可能发生在在另一个池中生成的非主进程中,并且不允许从子进程中生成进程,因此我们防止了这个问题。

关于python - 运行时错误 :freeze_support() on Mac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60691363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com