python多进程

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

posted on 2021-05-11 11:57  HolaWorld  阅读(58)  评论(1编辑  收藏  举报

导航