2016-12-13 115 views
1

我需要运行一堆混合随机/确定性反应网络模拟,其算法在class Markov中说明。编号喜欢并行执行,并将所有输出写入单个文件,这在进一步分析中很容易使用。现在即时将它存储在npz文件中。在从属进程中创建Markov的实例时,我得到的错误是:global name 'Markov' is not defined。所以问题是:我如何在我的slave进程中创建一个Markov的实例?该代码下面列出了更多(一般)问题。多处理,实例创建和函数实例的传递

import numpy as np 
import pathos.multiprocessing as mp 


class Markov(object): 
    def __init__(self,SRN_name,rates,stoich_mat): 
     self.S=stoich_mat 
     self.SRN_name=SRN_name #SRN = Stochastic Reaction Network 
     self.rates=rates 
     self.nr_reactions=rates.shape[1] 

def worker(SRN_name,rates,stoich_mat,init_state,tf,species_continuous): 
    result = [] 
    try: 
     sim=Markov(SRN_name=SRN_name,rates=rates,stoich_mat=stoich_mat) 
    except Exception as e: 
     print e 

    result=None #here some methods of sim are executed 

    return result 

def handle_output(result): 
    data=np.load("niks.npz").files 
    data.append(result) 
    np.savez("niks",data) 

if __name__ == '__main__': 
    def sinput(t,amplitude=6.0,period=0.05,offset=1.0): 
     return amplitude*np.sin(period*t)+amplitude+offset 
    phospho_cascade=np.array(
          [[ 0, 0, 0, 0, 0, 0, 0, 0], # input 
          [-1, 1, 0, 0, 0, 0, 0, 0]])# A 

    phospho_rates=np.array([(0.2,0),2.0],dtype=object,ndmin=2) 

    phspho_init=np.array([sinput,5.0],ndmin=2).T 

    tf=1.0 
    S_C=[0] 

    np.savez("niks",stoich_mat=phospho_cascade,rates=phospho_rates,init_state=phspho_init) 
    kwargs={"SRN_name":"niks","rates":phospho_rates,"stoich_mat":phospho_cascade,"init_state":rates,"tf":tf,"species_continuous":S_C} 
    pool = mp.Pool(processes=mp.cpu_count()) 
    for i in range(2): 
     pool.apply_async(worker,kwds=kwargs,callback=handle_output) 
    pool.close() 
    pool.join() 

谢谢!

回答

0

只是回答你的问题的第二部分,是的,有更简单的方法来做多处理。我建议再次尝试您的代码,但使用“线程”模块。线程示例如下所示:

import threading 

#Just define any functions you'd like to thread: 
def RandomFunction(): 
    print('Hello World') 

#Now create the thread(s) as so: 
threadName = threading.Thread(target = RandomFunction , args =()) 

#Now start the threads. You may also use threadName.join(), if you'd like to wait for a thread to complete, before continuing. 
threadName.start() 

使用相同的语法,可以根据需要创建多个函数的线程,并同时运行它们。相信我,这要容易得多! :)

+2

嘿,Id宁愿使用多处理,因为线程是由GIL限制。 – Patrickens