简体   繁体   English

Python 多处理:并行运行多个 for 循环的每次迭代

[英]Python Multiprocessing: Running each iteration of multiple for loops in parallel

I have two loops in python.我在 python 中有两个循环。 Here is some pseudo code.这是一些伪代码。 I would like to run both those functions and each iteration of each of those functions at the same time.我想同时运行这两个函数和每个函数的每次迭代。 So in this example, there would be 8 processes going on at once.所以在这个例子中,将有 8 个进程同时进行。 I know you can use "Process", but I just don't know how to incorporate an iterable.我知道你可以使用“Process”,但我只是不知道如何合并一个可迭代的。 Please let me know, thanks!请让我知道,谢谢!

import...

def example1(iteration):
    print('stuff')

def example2(iteration):
    print('stuff')

if __name__ == '__main__':  
    freeze_support()
    pool = multiprocessing.Pool(4)
    iteration = [1,2,3,4]
    pool.map(example1,iteration)

Assuming they don't need to be kicked off at exactly the same time I think map_async is what you want.假设他们不需要在同一时间启动,我认为map_async就是你想要的。

In the example bellow we can print the result from example2 before example1 has finished even though example1 was kicked off first.在下面的示例中,我们可以在example1 example2之前打印示例 2 的结果,即使example1先启动。

import multiprocessing
import time

def example1(iteration):
    time.sleep(1)
    return 1

def example2(iteration):
    return 2

if __name__ == '__main__':
    pool = multiprocessing.Pool(4)
    iteration = [1,2,3,4]
    result1 = pool.map_async(example1, iteration)
    result2 = pool.map_async(example2, iteration)
    print(result2.get())
    print(result1.get())

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

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