How do I parallelize a simple Python loop?

后端 未结 13 1326
北荒
北荒 2020-11-22 11:54

This is probably a trivial question, but how do I parallelize the following loop in python?

# setup output lists
output1 = list()
output2 = list()
output3 =          


        
13条回答
  •  长发绾君心
    2020-11-22 12:30

    very simple example of parallel processing is

    from multiprocessing import Process
    
    output1 = list()
    output2 = list()
    output3 = list()
    
    def yourfunction():
        for j in range(0, 10):
            # calc individual parameter value
            parameter = j * offset
            # call the calculation
            out1, out2, out3 = calc_stuff(parameter=parameter)
    
            # put results into correct output list
            output1.append(out1)
            output2.append(out2)
            output3.append(out3)
    
    if __name__ == '__main__':
        p = Process(target=pa.yourfunction, args=('bob',))
        p.start()
        p.join()
    

提交回复
热议问题