gpt4 book ai didi

Python多处理池: how to join the reasults in a parallel way?

转载 作者:行者123 更新时间:2023-12-01 03:15:16 25 4
gpt4 key购买 nike

我已阅读Python multiprocessing.Pool: when to use apply, apply_async or map?它很有用,但仍然有我自己的问题。在下面的代码中,我希望 result_list.append(result) 以并行方式,我希望 4 个处理器并行附加结果并将 4 个列表转换为 1 个列表。

import multiprocessing as mp
import time

def foo_pool(x):
time.sleep(2)
return x*x

result_list = []
def log_result(result):
# This is called whenever foo_pool(i) returns a result.
# result_list is modified only by the main process, not the pool workers.
result_list.append(result)

def apply_async_with_callback():
pool = mp.Pool(4)
for i in range(10):
pool.apply_async(foo_pool, args = (i, ), callback = log_result)
pool.close()
pool.join()
print(result_list)

if __name__ == '__main__':
apply_async_with_callback()

最佳答案

Multiprocessing pool将是您的选择。

以下是一些示例代码,希望对您有所帮助。您也可以查看我的另一个答案以查看更多详细信息。 How can I make my python code run faster

    from multiprocessing import Pool
import time

def foo_pool(x):
return x*x

def main():
pool = Pool(4)
sampleData = [x for x in range(9)]
results = pool.map(foo_pool, sampleData)
pool.close()
pool.join()
print(results)

if __name__ == '__main__':
main()

关于Python多处理池: how to join the reasults in a parallel way?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42499621/

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