Python:在启动不同核心中使用不同参数的Python程序时出错



在阅读了一个类似的问题(执行与并行不同参数的同一python程序(之后,某些用户在so中提出,我尝试了自己的代码来启动一个带有不同参数的Python程序内核。但是我遇到了麻烦。我的主要问题是,我的功能需要11个参数,当我以列表的形式提供这些参数时,它说我只通过一个参数。

from main_program import sliding_solver
from multiprocessing import Pool
import glob,shutil
from const_for_drdo_adaptive_split import *
#The file- const_for_drdo_adaptive defines all the parameters for used in this file
C1=[-5,-2.5,0,2.5,5];spr1=2.5;LR1=[10,20,30,30,30]
C2=[-4,-3,-2,-1,0,1,2,3,4];spr2=1;LR2=[30,40,50,50,50]
C3=[-4,-2,0,2,4];spr3=2;LR3=[40,50,60,60,60]
par=[[spr1,LR1,C1],[spr2,LR2,C2],[spr3,LR3,C3]]
#0,x0,1e-2,T,tim,So,C,spr,LR,num,param
init_cond=[]
for i in range(len(par)):
    theta11=np.zeros(5*len(par[i][2])**num)
    x0=np.concatenate((x10,theta11),axis=0) #x10 is an array of 9 elements
    init_cond.append(x0)
#theta1=np.zeros(5*len(C)**num)
#x0=np.concatenate((x10,theta1),axis=0 )
print x0
arg=[[0,init_cond[0],1e-2,T,tim,So,C1,spr1,LR1,num,param],[0,init_cond[1],1e-2,T,tim,So,C2,spr2,LR2,num,param],[0,init_cond[2],1e-2,T,tim,So,C3,spr3,LR3,num,param]]
if __name__=='__main__':
    pool=Pool(4)
    results=pool.map(sliding_solver,zip(arg[0],arg[1],arg[2]))
    for file in glob.glob("*_"+str(par[i][0])+"*"):
        shutil.move(file,"results")

我希望知道如何将11个参数的三个不同实例传递给我的功能,并以不同的核心启动。请帮助。预先感谢!

[ 0.5  0.   0.  ...,  0.   0.   0. ]
Traceback (most recent call last):
  File "parallel_adaptive_split.py", line 23, in <module>
    results=pool.map(sliding_solver,zip(arg[0],arg[1],arg[2]))
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map
    return self.map_async(func, iterable, chunksize).get()
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get
    raise self._value
TypeError: sliding_solver() takes exactly 11 arguments (1 given)

而不是使用池,而是通过创建三个不同的过程然后启动过程并最终加入它们来解决我的问题。

from main_program import sliding_solver
from multiprocessing import Pool
import glob,shutil
from const_for_drdo_adaptive_split import *
from multiprocessing import Process
C1=[-5,-2.5,0,2.5,5];spr1=2.5;LR1=[10,20,30,30,30]
C2=[-4,-3,-2,-1,0,1,2,3,4];spr2=1;LR2=[30,40,50,50,50]
C3=[-4,-2,0,2,4];spr3=2;LR3=[40,50,60,60,60]
par=[[spr1,LR1,C1],[spr2,LR2,C2],[spr3,LR3,C3]]
#0,x0,1e-2,T,tim,So,C,spr,LR,num,param
init_cond=[]
for i in range(len(par)):
    theta11=np.zeros(5*len(par[i][2])**num)
    x0=np.concatenate((x10,theta11),axis=0)
    init_cond.append(x0)
#theta1=np.zeros(5*len(C)**num)
#x0=np.concatenate((x10,theta1),axis=0 )
print x0
arg=[[0,init_cond[0],1e-2,T,tim,So,C1,spr1,LR1,num,param],[0,init_cond[1],1e-2,T,tim,So,C2,spr2,LR2,num,param],[0,init_cond[2],1e-2,T,tim,So,C3,spr3,LR3,num,param]]
if __name__=='__main__':
    #pool=Pool(3)
    #results=pool.map(sliding_solver,zip(arg[0],arg[1],arg[2]))
    p1=Process(target=sliding_solver,args=(0,init_cond[0],1e-2,T,tim,So,C1,spr1,LR1,num,param))
    p2=Process(target=sliding_solver,args=(0,init_cond[1],1e-2,T,tim,So,C2,spr2,LR2,num,param))
    p3=Process(target=sliding_solver,args=(0,init_cond[2],1e-2,T,tim,So,C3,spr3,LR3,num,param))
    p1.start()
    p2.start()
    p3.start()
    p1.join()
    p2.join()
    p3.join()
    for file in glob.glob("*_"+str(arg[i][0])+"*"):
        shutil.move(file,"results")

相关内容

  • 没有找到相关文章

最新更新