我正在尝试在多处理池中的每个进程上运行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)”在探查器中不会影响调用范围中的“结果”。我怎样才能完成我想在这里做的事情?

有帮助吗?

解决方案

试试这个:

def square_wrapper(i):
    result = [None]
    cProfile.runctx("result[0] = square(i)", globals(), locals(), "file_%d" % i)
    return result[0]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top