2012-04-02 68 views
0

我使用的第一個回答這個問題可用內存多處理

Overcoming Python's limitations regarding instance methods

醃製方法,能夠使用多進程模塊上我自己的一個類的方法。

舉個例子,讓我們說,我有以下幾點:

from multiprocessing import Pool 

def myParallelFunc(my_list, a, b, inst): 
    # do something 
    return True 

def myFunc: 
    # instantiate custom class 
    my_instance = MyObject() 

    pool = Pool() 
    pool.map(functools.partial(myParallelFunc, a=5, b=7, inst=my_instance), my_list) 

    # SOLUTION!!! 
    pool.close() 

現在我有另一個程序調用myFunc的假設100倍。 每次我打電話給myFunc時,一些內存都被佔用,永遠不會釋放。 有沒有辦法明確釋放它?

+0

你怎麼知道它是永遠不會被釋放? Python內存管理應該爲你處理事情。 – Pierce 2012-04-02 11:15:16

+0

我使用「top」進行監控,並且只在最後發佈,當程序退出時 – 2012-04-02 11:22:29

回答

1

您在每次調用myFunc時都會創建一個新Pool。當myFunc退出時它不會自動刪除,因爲子進程和相關的線程依然存在。

創建池,保持它周圍的100個電話,然後.close它