class NoDaemonProcess(multiprocessing.Process):
# make 'daemon' attribute always return False
@property
def daemon(self):
return False
@daemon.setter
def daemon(self, val):
pass
class NoDaemonProcessPool(multiprocessing.pool.Pool):
def Process(self, *args, **kwds):
proc = super(NoDaemonProcessPool, self).Process(*args, **kwds)
proc.__class__ = NoDaemonProcess
return proc
在NoDaemonProcessPool类中,会引发一个错误,看起来像是python无法找到multiprocessing.pool
class NoDaemonProcessPool(multiprocessing.pool.Pool):
AttributeError: module 'multiprocessing' has no attribute 'pool'. Did you mean: 'Pool'?
我不知道如何解决这个问题。
默认不优先导入multiprocessing
包的pool
子模块。相反,它是在第一次调用multiprocessing.Pool
时加载的。如果您想从multiprocessing.pool.Pool
继承,您需要自己显式地导入子模块,否则它可能无法工作。
试题:
import multiprocessing.pool # add the submodule here!
class NoDaemonProcessPool(multiprocessing.pool.Pool):
...