简体   繁体   English

使用多个参数运行相同 function 的多处理 Python

[英]Run Python Multiprocessing of same function with multiple parameters

I have a function say:我有一个 function 说:

 Fun1(a,b):
   return a*b

I want to call Fun1, multiple times with different values of a and b.我想用不同的 a 和 b 值多次调用 Fun1。 Need to use multiprocessing as Fun1 is an called millions of time in the需要使用多处理,因为 Fun1 是一个叫做百万次的时间

from concurrent.futures import ProcessPoolExecutor


def fun1(a, b):
    return a * b

# if __name__ == '__main__': is required for platforms that do not
# support fork(), such as Windows:
if __name__ == '__main__':
    a = [1, 2, 3]
    b = [4, 5, 6]
    with ProcessPoolExecutor(max_workers = 3) as executor:
        results = executor.map(fun1, a, b)
    for result in results:
        print(result)
4
10
18

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM