Python多处理-进程完成后如何释放内存? [英] Python multiprocessing - How to release memory when a process is done?

查看:2361
本文介绍了Python多处理-进程完成后如何释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用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号)被挂起¹.它们不占用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 suspended¹ 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?

谢谢!

¹由暂停"表示我在ps命令中的状态显示为"S +"

¹ 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;

推荐答案

您是否尝试过使用

Did you try to close pool by using pool.close and then wait for process to finish by pool.join, because if parent process keeps on running and does not wait for child processes they will become zombies

这篇关于Python多处理-进程完成后如何释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆