多处理,实例创建和函数实例的传递

时间:2016-12-13 20:34:07

标签: python class multiprocessing

我需要使用class Markov中所述的算法运行一系列反应网络的混合随机/确定性模拟。我喜欢并行执行此操作,并将所有输出写入单个文件,这在进一步分析中很容易使用。现在我将它存储在npz文件中。在从属进程中创建Markov的实例时,我得到的错误是:global name 'Markov' is not defined。所以问题是:如何在我的奴隶进程中创建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()

谢谢!

1 个答案:

答案 0 :(得分: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()

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