gpt4 book ai didi

python - `Pool.*_async` 功能从未准备好

转载 作者:行者123 更新时间:2023-12-01 02:34:06 30 4
gpt4 key购买 nike

import threading
import multiprocessing.dummy as mt
import numpy as np


if __name__ == '__main__':
n = 6
a = np.zeros((n, n))

def f(i, j):
a[i, j] = i + j

with mt.Pool() as pool:
r = pool.starmap_async(f, ((i, j) for i in range(n) for j in range(n)))

r.wait()
print(a)

上面的代码片段将在 r.wait() 处自行阻塞。但如果将其更改为

import threading
import multiprocessing.dummy as mt
import numpy as np


if __name__ == '__main__':
n = 6
a = np.zeros((n, n))

def f(i, j):
a[i, j] = i + j

with mt.Pool() as pool:
pool.starmap(f, ((i, j) for i in range(n) for j in range(n)))

print(a)

a的内容将立即打印。那么为什么第一个片段中的 r 从未准备好呢? (Python 版本:Python 3.6.2::Anaconda 自定义(64 位),Linux 下)

最佳答案

问题是您在 with block 之外等待。一旦退出 with block ,池就是 terminated ,这会阻止您完成任何任务。来自javadoc:

Pool objects now support the context management protocol – see Context Manager Types. __enter__() returns the pool object, and __exit__() calls terminate().

...

terminate()

Stops the worker processes immediately without completing outstanding work. When the pool object is garbage collected terminate() will be called immediately.

将调用移至 with block 内的 r.wait() 可以修复该问题。

关于python - `Pool.*_async` 功能从未准备好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46451262/

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