python进程池pool的starmap的使用

#!/usr/bin/env python3
from functools import partial
from itertools import repeat
from multiprocessing import Pool, freeze_support

def func(a, b):
    return a + b

def main():
    a_args = [1,2,3]
    second_arg = 1
    with Pool() as pool:
        L = pool.starmap(func, [(1, 1), (2, 1), (3, 1)])
        M = pool.starmap(func, zip(a_args, repeat(second_arg)))
        N = pool.map(partial(func, b=second_arg), a_args)
        assert L == M == N

if __name__=="__main__":
    freeze_support()
    main()

原文看这里:https://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments

  

 

from functools import partial
from itertools import repeat
from multiprocessing import Pool, freeze_support


def func(a, b, c):
    print(c)
    return a + b


def main():
    a_args = [1, 2, 3]
    second_arg = 1
    with Pool() as pool:
        # L = pool.starmap(func, [(1, 1), (2, 1), (3, 1)])
        # M = pool.starmap(func, zip(a_args, repeat(second_arg)))
        N = pool.map(partial(func, b=second_arg,c="124"), a_args)


if __name__ == "__main__":
    freeze_support()
    main()

  

posted @ 2018-05-17 09:11  公众号python学习开发  阅读(10141)  评论(0编辑  收藏  举报