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

后端 未结 6 719
梦毁少年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:00

    You need to remove the * on the map call:

    args = ((a, b) for b in c)
    for result in executor.map(f, args):
        pass
    

    This will call f, len(args) times, where f should accept one parameter.

    If you want f to accept two parameters you can use a lambda call like:

    args = ((a, b) for b in c)
    for result in executor.map(lambda p: f(*p), args):   # (*p) does the unpacking part
        pass
    

提交回复
热议问题