分析 python 多处理池
我试图在多处理池中的每个进程上运行 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)”不会影响调用范围内的“结果”。我怎样才能完成我在这里想做的事情?
I'm trying to run cProfile.runctx() on each process in a multiprocessing pool, to get an idea of what the multiprocessing bottlenecks are in my source. Here is a simplified example of what I'm trying to do:
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
Unfortunately, trying to execute "result = square(i)" in the profiler does not affect 'result' in the scope it was called from. How can I accomplish what I am trying to do here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: