from multiprocess import Pool, cpu_count
with Pool() as pool:
# input_list: [input1, input2, input3...]
for output in pool.imap_unordered(func, input_list):
# do something
或
pool = Pool(processes=cpu_count()-1)
for output in pool.imap_unordered(func, input_list):
# do something
def func(input):
# do something
return output
from concurrent.futures import ProcessPoolExecutor
def func(input1, input2):
# do something
return output
input1 = [1, 2, 3]
input2 = [4, 5, 6]
with ProcessPoolExecutor() as executor:
list(executor.map(func, input1, input2))
或
for _ in executor.map(func, input1, input2):
pass