我有一段代码正在使用 pathos multiprocessing 编写,这会产生错误。问题是使用 pathos.multiprocessing.pool 在一组对象上分发进程似乎在与类定义中初始化的对象不同的对象上完成工作。
class Training_Set:
def __init__( self, configurations ):
print("Train init",configurations)
self.configurations = configurations
self.forces = np.concatenate( [ c.reference_forces for c in configurations ] )
self.dipoles = np.concatenate( [ c.reference_dipoles for c in configurations ] )
self.stresses = np.concatenate( [ c.reference_stresses for c in configurations ] )
def run( self, config ):
print("Train: run config",config)
ran_okay = config.run( clean = True)
if not ran_okay:
return( False )
return( True )
def run_multi(self):
pool_size = mp.cpu_count()
pool = mp.Pool(processes=pool_size,maxtasksperchild=1,)
print("Train run_multi: configs",self.configurations)
pool_outputs = pool.map(self.run, self.configurations)
pool.close()
pool.join()
if "False" in pool_outputs:
return( False)
self.ran_okay = False
else:
return( True)
self.ran_okay = True
@property
def new_forces( self ):
print("Train: New forces config",self.configurations)
return np.concatenate( [ c.new_forces for c in self.configurations ] )
在和 in__init__
中初始化的配置对象是相同的。所以这些配置是在调用中使用的那些:run_multi()
new_forces
pool.map
pool_outputs = pool.map(self.run, self.configurations)
但是定义中引用的对象run
占用了不同的内存部分。
任何可能导致这种情况的想法将不胜感激!
谢谢!