【发布时间】: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