使用并行python使python函数并行执行

2024-09-29 21:40:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉并行处理。我写了一个脚本,需要很多时间来执行,所以我想让它的功能在CPU内核上并行运行。为此,我使用了并行python模块。我已经进口了

import pp
ppservers = ()

if len(sys.argv) > 1:
   ncpus = int(sys.argv[1])    
   job_server = pp.Server(ncpus, ppservers=ppservers)
else:    
    job_server = pp.Server(ppservers=ppservers)

我想让它并行执行的函数是:

def Parallel_Solution_Combination_Method(subset):

传递给函数的参数子集是两个染色体对象的列表。染色体对象是我在上面定义的同一个脚本中构建的类。你知道吗

class chromosome():    
def __init__(self):        
    self.record_template = ''
    self.record_target = ''
    self.molpdf_score = 0.0
    self.ga341_score = 0.0
    self.dope_score = 0.0
    self.normalized_dope_score = 0.0
    self.flag_value = 0
    self.distance_value = 0   
def add_molpdf(self, molpdf):
    self.molpdf_score = molpdf
def add_ga341(self, ga341):
    self.ga341_score = ga341
def add_dope(self, dope):
    self.dope_score = dope
def add_normalized_dope(self, normalized_dope):
    self.normalized_dope_score = normalized_dope
def add_records(self, records):
    self.seq_records = records
    for rec in self.seq_records:            
        if rec.id == template_id:                 
            self.record_template = rec.seq
        elif rec.id == target_id:
            self.record_target = rec.seq  
def set_flag(self, flag):
    self.flag_value = flag
def add_distance(self, distance):
    self.distance_value = distance

subset参数包含两个染色体的列表(上面定义的类)。现在,我希望它并行执行的函数(即parallel\u Solution\u Combination\u Method)依次调用两个函数:

find_allIndices
find_different_indicies

稍后我将使用此脚本尝试并行运行该函数

jobs = [(subset, job_server.submit(Parallel_Solution_Combination_Method,
        (subset,), (find_allIndices,find_different_indicies,),("random",))) 
        for subset in subsets
       ]

每当我运行这个for循环时:

for subset, job in jobs:
    print subset, job()

我得到这个错误:

(<__main__.chromosome instance at 0x000000000A15FA88>, <__main__.chromosome instance at 0x000000000A2D4C88>) 
A fatal error has occured during the function execution
Traceback (most recent call last):
  File "C:\Users\mossig\AppData\Local\Continuum\Anaconda\lib\site-  packages\ppworker.py", line 86, in run
    __args = pickle.loads(__sargs)
AttributeError: 'module' object has no attribute 'chromosome' None

您可能注意到for循环正在打印染色体实例,但是在我看来PP无法识别染色体对象。我不知道错误是怎么造成的。我尝试在作业的modules参数中添加染色体类_服务器.提交结果仍然不一样。你知道吗

请帮忙!!非常感谢。你知道吗

def Parallel_Solution_Combination_Method(subset):

print 'entered parallel sol comb'
child_chromosome = chromosome()   
combination_model_offset = 300

attempts = 0
while True:

        template1 = subset[0].record_template
        template2 = subset[1].record_template
        template_child = template1

        template_gap1 = find_allIndices(template1, '-')
        template_gap2 = find_allIndices(template2, '-')

        if(len(template_gap1) !=0 and len(template_gap2) != 0):
            template_gap_difference = find_different_indicies(template_gap1, template_gap2)
            if(len(template_gap_difference) != 0):        
                template_slice_point = random.choice(template_gap_difference)

                if(template_gap2[template_slice_point -1] < template_gap1[template_slice_point]):
                    #swap template1 template2 values as well as their respective gap indices
                    #so that in crossover the gaps would not collide with each other.
                    temp_template = template1
                    temp_gap = template_gap1

                    template1 = template2
                    template2  = temp_template
                    template_gap1 = template_gap2
                    template_gap2 = temp_gap

                #the crossing over takes the first part of the child sequence to be up until
                #the crossing point without including it. this way it ensures that the resulting
                #child sequence is different from both of the parents by at least one point.

                child_template_gap = template_gap1[:template_slice_point]+template_gap2[template_slice_point:]
                child_gap_part1 = child_template_gap[:template_slice_point]
                child_gap_part2 = child_template_gap[template_slice_point:]           

                if template_slice_point == 0:
                    template_child = template2
                else:
                    template_child = template1[:template_gap1[template_slice_point]]            

                    template_residues_part1 = str(template_child).translate(None, '-')
                    template_residues_part2 = str(template2).translate(None, '-')
                    template_residues_part2 = template_residues_part2[len(template_residues_part1):]


                    for i in range(template_gap1[template_slice_point-1], len(template1)):
                        if i in child_gap_part2:
                            template_child = template_child + '-'
                        else:
                            template_child = template_child + template_residues_part2[0:1]
                            template_residues_part2 = template_residues_part2[1:]



        target1 = subset[0].record_target
        target2 = subset[1].record_target
        target_child = target1

        target_gap1 = find_allIndices(target1, '-')
        target_gap2 = find_allIndices(target2, '-')    

        if(len(target_gap1) !=0 and len(target_gap2) != 0):
            target_gap_difference = find_different_indicies(target_gap1, target_gap2)
            if(len(target_gap_difference) !=0):
                target_slice_point = random.choice(target_gap_difference)        
                if(target_gap2[target_slice_point -1] < target_gap1[target_slice_point]):
                    #swap template1 template2 values as well as their respective gap indices
                    #so that in crossover the gaps would not collide with each other.
                    temp_target = target1
                    temp_gap = target_gap1

                    target1 = target2
                    target2  = temp_target
                    target_gap1 = target_gap2
                    target_gap2 = temp_gap

                #the crossing over takes the first part of the child sequence to be up until
                #the crossing point without including it. this way it ensures that the resulting
                #child sequence is different from both of the parents by at least one point.

                child_target_gap = target_gap1[:target_slice_point]+target_gap2[target_slice_point:]
                child_gap_part1 = child_target_gap[:target_slice_point]
                child_gap_part2 = child_target_gap[target_slice_point:]           

                if target_slice_point == 0:
                    target_child = target2
                else:
                    target_child = target1[:target_gap1[target_slice_point]]            

                    target_residues_part1 = str(target_child).translate(None, '-')
                    target_residues_part2 = str(target2).translate(None, '-')
                    target_residues_part2 = target_residues_part2[len(target_residues_part1):]


                    for i in range(target_gap1[target_slice_point-1], len(target1)):
                        if i in child_gap_part2:
                            target_child = target_child + '-'
                        else:
                            target_child = target_child + target_residues_part2[0:1]
                            target_residues_part2 = target_residues_part2[1:]    

        if not [False for y in Reference_Set if y.record_template == template_child and y.record_target == target_child] or attempts <= 100:
            break
        attempts +=1


    child_chromosome.record_template = template_child    
    child_chromosome.record_target = target_child    
    generate_PIR(template_header, template_description,   child_chromosome.record_template, target_header,target_description, child_chromosome.record_target)

    output_values = start_model(template_id, target_id,'PIR_input.ali', combination_model_offset)
    child_chromosome.molpdf_score = output_values['molpdf']    
    mdl = complete_pdb(env, '1BBH.B99990300.pdb')
    child_chromosome.normalized_dope_score = mdl.assess_normalized_dope()    

    return child_chromosome

后来我也试着这样运行:

fn = pp.Template(job_server, Parallel_Solution_Combination_Method,
                 (find_allIndices,find_different_indicies, generate_PIR, 
                 start_model,),("random", "modeller", "Bio",)
                )
job1 = fn.submit(subsets[0])

result = job1()

我有个错误:

A fatal error has occured during the function execution
Traceback (most recent call last):
  File "C:\Users\mossig\AppData\Local\Continuum\Anaconda\lib\site-packages\ppworker.py", line 86, in run
    __args = pickle.loads(__sargs)
AttributeError: 'module' object has no attribute 'chromosome'

Tags: theselfchildtargetifslicetemplaterecord

热门问题