Python multiprocessing - How to release memory when a process is done?(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;
推荐答案
您是否尝试使用 pool.close 然后等待进程完成 pool.join,因为如果父进程继续运行并且不等待子进程他们会变成僵尸
这篇关于Python多处理 - 进程完成后如何释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python多处理 - 进程完成后如何释放内存?
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
