Python 多处理-进程完成后如何释放内存?

发布于 2024-12-08 05:28:09 字数 624 浏览 1 评论 0原文

我在使用 python 多处理库时遇到了一个奇怪的问题。

我的代码如下所示:我为每个“符号,日期”元组生成一个过程。之后我将结果结合起来。

我期望当一个进程完成“符号,日期”元组的计算时,它应该释放其内存?显然事实并非如此。我看到机器中有数十个进程(尽管我将进程池设置为大小 7)被挂起。它们不消耗CPU,也不释放内存。

在完成计算后,如何让进程释放其内存?

谢谢!

1 “暂停”是指它们在 ps 命令中的状态显示为“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;

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.

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?

Thanks!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窝囊感情。 2024-12-15 05:28:09

您是否尝试使用 pool.close 来关闭池。关闭,然后等待 pool.join,因为如果父进程继续运行并且不等待子进程,它们将变成 僵尸

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

深海少女心 2024-12-15 05:28:09

尝试在池上设置 maxtasksperchild 参数。如果不这样做,那么池会一遍又一遍地重用该进程,因此内存永远不会被释放。设置后,该进程将被允许终止,并在其位置创建一个新进程。这将有效地清理内存。

我想它是 2.7 中的新功能: http://docs.python.org/2/library /multiprocessing.html#module-multiprocessing.pool

Try setting the maxtasksperchild argument on the pool. If you don't, then the process is reusued over and over again by the pool so the memory is never released. When set, the process will be allowed to die and a new one created in it's place. That will effectively clean up the memory.

I guess it's new in 2.7: http://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.pool

梦醒灬来后我 2024-12-15 05:28:09

您可能应该在 Pool 对象上调用 close(),然后调用 wait()

http://docs.python.org/library/multiprocessing.html#module -multiprocessing.pool

加入()
等待工作进程退出。在使用 join() 之前必须调用 close() 或 Terminate()。

You should probably call close() followed by wait() on your Pool object.

http://docs.python.org/library/multiprocessing.html#module-multiprocessing.pool

join()
Wait for the worker processes to exit. One must call close() or terminate() before using join().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文