python进程池pool的starmap的使用

Posted 一起来学python

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()

  



以上是关于python进程池pool的starmap的使用的主要内容,如果未能解决你的问题,请参考以下文章

Python Pool.Starmap 未在打印时终止或输出

Python 多线程池 Windows 冻结

如何将两个参数传递给 Pool.starmap()?

python 进程池pool简单使用

如何从 Pool.starmap_async() 中获取结果?

python 进程池pool