gpt4 book ai didi

python - 分析 python 多处理池

转载 作者:太空狗 更新时间:2023-10-29 21:48:00 25 4
gpt4 key购买 nike

我正在尝试在多处理池中的每个进程上运行 cProfile.runctx(),以了解我的源中的多处理瓶颈是什么。这是我正在尝试做的一个简化示例:

from multiprocessing import Pool
import cProfile

def square(i):
return i*i

def square_wrapper(i):
cProfile.runctx("result = square(i)",
globals(), locals(), "file_"+str(i))
# NameError happens here - 'result' is not defined.
return result

if __name__ == "__main__":
pool = Pool(8)
results = pool.map_async(square_wrapper, range(15)).get(99999)
print results

不幸的是,尝试在探查器中执行“result = square(i)”不会影响调用范围内的“result”。我怎样才能完成我想在这里做的事情?

最佳答案

试试这个:

def square_wrapper(i):
result = [None]
cProfile.runctx("result[0] = square(i)", globals(), locals(), "file_%d" % i)
return result[0]

关于python - 分析 python 多处理池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1414841/

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