Pythoncurrent.futures:ProcessPoolExecutor无法正常工作

问题描述 投票:0回答:1

我正在尝试使用ProcessPoolExecutor方法,但失败。这是一个使用失败的示例(计算两个数字的大除法)。我不明白这是什么错误

def gcd(pair):
    a, b = pair
    low = min(a, b)
    for i in range(low, 0, -1):
        if a % i == 0 and b % i == 0:
            return i

numbers = [(1963309, 2265973), (2030677, 3814172),
           (1551645, 2229620), (2039045, 2020802)]
start = time()
pool = ProcessPoolExecutor(max_workers=2)
results = list(pool.map(gcd, numbers))
end = time()
print('Took %.3f seconds' % (end - start))

BrokenProcessPool:在将来运行或挂起时,进程池中的进程突然终止。

python time concurrency subprocess concurrent.futures
1个回答
0
投票

将您的代码更改为如下所示,它将起作用:

from time import time
from concurrent.futures import ProcessPoolExecutor
def gcd(pair):
    a, b = pair
    low = min(a, b)
    for i in range(low, 0, -1):
        if a % i == 0 and b % i == 0:
            return i

numbers = [(1963309, 2265973), (2030677, 3814172),
           (1551645, 2229620), (2039045, 2020802)]

def main():
    start = time()
    pool = ProcessPoolExecutor(max_workers=3)
    results = list(pool.map(gcd, numbers))
    end = time()
    print('Took %.3f seconds' % (end - start))


if __name__ == '__main__':
    main()
© www.soinside.com 2019 - 2024. All rights reserved.