How do I parallelize a simple Python loop?

后端 未结 13 1294
北荒
北荒 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:26

    I found joblib is very useful with me. Please see following example:

    from joblib import Parallel, delayed
    def yourfunction(k):   
        s=3.14*k*k
        print "Area of a circle with a radius ", k, " is:", s
    
    element_run = Parallel(n_jobs=-1)(delayed(yourfunction)(k) for k in range(1,10))
    

    n_jobs=-1: use all available cores

提交回复
热议问题