Pass multiple parameters to concurrent.futures.Executor.map?

后端 未结 6 718
梦毁少年i
梦毁少年i 2020-12-07 17:27

The concurrent.futures.Executor.map takes a variable number of iterables from which the function given is called. How should I call it if I have a generator that pro

6条回答
  •  隐瞒了意图╮
    2020-12-07 18:04

    For ProcessPoolExecutor.map():

    Similar to map(func, *iterables) except:

    the iterables are collected immediately rather than lazily;

    func is executed asynchronously and several calls to func may be made concurrently.

    Try running the following snippet under python 3, and you will be quite clear:

    from concurrent.futures import ProcessPoolExecutor
    
    def f(a, b):
        print(a+b)
    
    with ProcessPoolExecutor() as pool:
        pool.map(f, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2))
    
    # 0, 2, 4
    
    array = [(i, i) for i in range(3)]
    with ProcessPoolExecutor() as pool:
        pool.map(f, *zip(*array))
    
    # 0, 2, 4
    

提交回复
热议问题