我是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])
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,发生了这个错误:
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.
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
如果您将所有内容放在此if __name__ == "__main__"块中的全局范围内,如下所示,您应该会发现您的程序按照您的预期运行:
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])
Run Code Online (Sandbox Code Playgroud)
如果没有这种保护,如果您当前的模块是从不同的模块导入的,您的多处理代码将被执行。这可能发生在另一个池中生成的非主进程中,并且不允许从子进程生成进程,因此我们防止出现此问题。
| 归档时间: |
|
| 查看次数: |
1935 次 |
| 最近记录: |