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()

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM