我有一段代码我正在使用pathos多处理编写,这会产生错误。问题是在一组对象上使用pathos.multiprocessing.pool分发进程似乎是在与类定义中初始化的对象不同的对象上进行的。
plpgsql
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 ] )
,__init__
和run_multi()
中初始化的配置对象是相同的。因此,这些配置是在new_forces
的调用中使用的配置:
pool.map
但是 pool_outputs = pool.map(self.run, self.configurations)
定义中引用的对象占用了内存的不同部分。
任何可能导致这种情况的想法都会受到赞赏!
谢谢!