問題描述
我在使用 python 多處理庫時遇到了一個奇怪的問題.
I encountered a weird problem while using python multiprocessing library.
我的代碼如下所示:我為每個符號、日期"元組生成一個進程.之后我結合結果.
My code is sketched below: I spawn a process for each "symbol, date" tuple. I combine the results afterwards.
我希望當一個進程完成對符號,日期"元組的計算時,它應該釋放它的內存嗎?顯然情況并非如此.我看到幾十個進程(盡管我將進程池的大小設置為 7)在機器中掛起1.它們不消耗 CPU,也不釋放內存.
I expect that when a process has done computing for a "symbol, date" tuple, it should release its memory? apparently that's not the case. I see dozens of processes (though I set the process pool to have size 7) that are suspended1 in the machine. They consume no CPU, and they don't release the memory.
如何讓進程在完成計算后釋放其內存?
How do I let a process release its memory, after it has done its computation?
謝謝!
1暫停"是指它們在 ps 命令中的狀態顯示為S+"
1 by "suspended" I mean their status in ps command is shown as "S+"
def do_one_symbol( symbol, all_date_strings ):
pool = Pool(processes=7)
results = [];
for date in all_date_strings:
res = pool.apply_async(work, [symbol, date])
results.append(res);
gg = mm = ss = 0;
for res in results:
g, m, s = res.get()
gg += g;
mm += m;
ss += s;
推薦答案
您是否嘗試使用 pool.close
然后等待進程完成 pool.join
,因為如果父進程繼續運行并且不等待子進程他們會變成僵尸
這篇關于Python多處理 - 進程完成后如何釋放內存?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!