简体   繁体   中英

Python how to pass list of argument pair in executor.map

What I have:

ids = [1,2,3...]
dates = ['a', 'b', 'c'...]

def f(id, date):
  print(f'{id} - {date}')

What I intend to do: Run f in multithreads with every combination of id, dates as arguments

Expected output:

1 - a
1 - b
1 - c
2 - a
2 - b
2 - c
3 - a
3 - b
3 - c 
...

This obviously doesn't work

with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    executor.map(f, ids, dates)

This doesn't work either and looks clumsy: first use loops to build a list of arguments combo like

args = [[1,a][1,b][1,c]
        [2,a],[2,b],[2,c]
        ...
       ]

and pass it to f:

    executor.map(f, args)

or

    executor.map(f, *args) 

either failed

Or redesign f so it takes only one argument?

There must a nice way...

.map() will iterate all iterables in parallel. It won't try to find all combinations:

If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.

A simple fix would be to use itertools.product . There is no nicer and readable way I can think of:

Change your function to:

def f(t):
  id, date = t
  print(f'{id} - {date}')

then:

import itertools
...
    executor.map(f, itertools.product(ids, dates))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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