简体   繁体   English

分析python多处理池

[英]Profiling a python multiprocessing pool

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. 我正在尝试在多处理池中的每个进程上运行cProfile.runctx(),以了解我的源中的多处理瓶颈。 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. 不幸的是,尝试在探查器中执行“result = square(i)”不会影响调用它的范围内的“结果”。 How can I accomplish what I am trying to do here? 我怎样才能完成我想在这里做的事情?

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM