簡體   English   中英

分析python多處理池

[英]Profiling a python multiprocessing pool

我正在嘗試在多處理池中的每個進程上運行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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM