【问题标题】:How to pass multiple arguments to concurrent.futures.ThreadPoolExecutor.map() function?如何将多个参数传递给 concurrent.futures.ThreadPoolExecutor.map() 函数?
【发布时间】:2021-09-02 16:33:29
【问题描述】:

我正在编写一个程序来下载具有多线程的图像 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() 函数?

【问题讨论】:

    标签: python multithreading concurrent.futures


    【解决方案1】:

    使用functools.partial:

    from functools import partial
    
    images = []
    path = "/home/dead101/Downloads"
    
    with concurrent.futures.ThreadPoolExecutor() as executor:
        executor.map(partial(down, path=path), images)
    

    【讨论】:

      【解决方案2】:

      你可以使用

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

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 2016-04-02
        • 1970-01-01
        • 2016-01-17
        • 1970-01-01
        • 2020-05-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-13
        相关资源
        最近更新 更多