如何将多个参数传递给 concurrent.futures.ThreadPoolExecutor.map() 函数?

How to pass multiple arguments to concurrent.futures.ThreadPoolExecutor.map() function?

我正在编写一个程序来使用多线程下载图像列表 url。我的函数如下所示。

from wget import download

def down(url, path=None):
    """Download image urls.
    :param url: url of the image(mandatory)
    :param path: path to be downloaded images(optional) 
    """
    if path != None:    # if path is provided
        try:
            chdir(path) # change directory
            download(url)
        except ValueError:    # excepting urls that aren't images
            pass
    else:   # if path is not provided
        # again search for non-image urls and except them   
        try:
            download(url)
        except: 
            pass

我可以通过 url 下载图片,无需路径如下。

images = [] # list of image urls

# multithreading downloading
with concurrent.futures.ThreadPoolExecutor() as executor:
    executor.map(down, images)

但是当我尝试如下传递路径时,它什么也不做(不将图像下载到路径中)。

images = []
path = "/home/dead101/Downloads"

with concurrent.futures.ThreadPoolExecutor() as executor:
    executor.map(lambda p: down(*p), (images, path))

所以我的问题是如何将具有多个参数的函数传递给 executor.map() 函数?

使用functools.partial:

from functools import partial

images = []
path = "/home/dead101/Downloads"

with concurrent.futures.ThreadPoolExecutor() as executor:
    executor.map(partial(down, path=path), images)

您可以使用

executor.map(lambda p: down(*p), [(i, path) for i in images] )