Parallel for loop

一个简单的例子

from math import sqrt
from joblib import Parallel, delayed

# single-core code
sqroots_1 = [sqrt(i ** 2) for i in range(10)]

# parallel code
sqroots_2 = Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))

带输入的函数的并行

from joblib import Parallel, delayed
import multiprocessing
     
# a single task
inputs = range(10) 
def processInput(i):
    return i * i
 
num_cores = multiprocessing.cpu_count()
results = Parallel(n_jobs=num_cores)(delayed(processInput)(i) for i in inputs)

常见的几种编程语言的for循环并行的用法https://blog.dominodatalab.com/simple-parallelization/

你可能感兴趣的:(Parallel for loop)