Python 池后的持久进程
我有一个 Python 程序,需要大约 10 分钟才能执行。因此,我使用 multiprocessing
中的 Pool
来加快速度:
from multiprocessing import Pool
p = Pool(processes = 6) # I have an 8 thread processor
results = p.map( function, argument_list ) # distributes work over 6 processes!
仅此而已,它的运行速度要快得多。上帝保佑Python!所以我想就这样了。
但是我注意到,每次执行此操作时,即使 p
超出范围,进程及其相当大的状态仍然保留;实际上,我造成了内存泄漏。这些进程在我的系统监视器应用程序中显示为 Python 进程,此时不使用 CPU,而是使用大量内存来维护其状态。
池具有close
、terminate
和join
函数,我假设其中一个函数会终止进程。有谁知道告诉我的池 p
我已经完成的最佳方式是什么?
非常感谢您的帮助!
I have a Python program that takes around 10 minutes to execute. So I use Pool
from multiprocessing
to speed things up:
from multiprocessing import Pool
p = Pool(processes = 6) # I have an 8 thread processor
results = p.map( function, argument_list ) # distributes work over 6 processes!
It runs much quicker, just from that. God bless Python! And so I thought that would be it.
However I've noticed that each time I do this, the processes and their considerably sized state remain, even when p
has gone out of scope; effectively, I've created a memory leak. The processes show up in my System Monitor application as Python processes, which use no CPU at this point, but considerable memory to maintain their state.
Pool has functions close
, terminate
, and join
, and I'd assume one of these will kill the processes. Does anyone know which is the best way to tell my pool p
that I am finished with it?
Thanks a lot for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 Python 文档 看来,您需要执行以下操作:
在
map()
之后指示工作人员应该终止,然后等待它们这样做。From the Python docs, it looks like you need to do:
after the
map()
to indicate that the workers should terminate and then wait for them to do so.