我有一个脚本,其中我使用pool.apply_async运行一些进程,并将它们作为非守护进程运行,以避免出现“僵尸”进程使内存不堪重负的问题。到目前为止,它一直运行良好,只是现在我已经扩展到内存中的更大数据集,因此通过使用所有内核,我可以明智地消耗内存。我想限制在这种情况下使用的内核数量,但不能使其正常工作
通常我会整合以下内容
pool = Pool(self.nb_cores)
限制内核数。但是,我似乎无法找出将其集成到非守护进程中的位置。
import multiprocessing
import multiprocessing.pool
class NoDaemonProcess(multiprocessing.Process):
"""
Extends the multiprocessing Process class to disable
the daemonic property. Polling the daemonic property
will always return False and cannot be set.
"""
@property
def daemon(self):
"""
Always return False
"""
return False
@daemon.setter
def daemon(self, value):
"""
Pass over the property setter
:param bool value: Ignored setting
"""
pass
class NoDaemonContext(type(multiprocessing.get_context())):
"""
With the new multiprocessing module, everything is based
on contexts after the overhaul. This extends the base
context so that we set all Processes to NoDaemonProcesses
"""
Process = NoDaemonProcess
class NoDaemonPool(multiprocessing.pool.Pool):
"""
This extends the normal multiprocessing Pool class so that
all spawned child processes are non-daemonic, allowing them
to spawn their own children processes.
"""
def __init__(self, *args, **kwargs):
kwargs['context'] = NoDaemonContext()
super(NoDaemonPool, self).__init__(*args, **kwargs)
我知道我需要在某个地方集成一些内核限制...似乎无法在我的上下文中找到我需要的精确功能。
答案 0 :(得分:0)
您的自定义kind='merge-sort'
类是从multiprocessing.pool.Pool
派生的,因此将能够接受NoDaemonPool
(要使用的工作进程数)作为关键字参数:
processes