并行快速下载图像的更好选择-> multiprocessing.Pool10或ThreadPool10以及如何在地图内部传递多个args

问题描述

我有一个功能齐全的脚本可供下载,但问题是这需要很多时间。我发现了一些实现,因此尝试实现自己的多处理。我发现2实现。对于第一部分,我想作为哪个更好?

def downloader_function(url,OUT_DIR,img_name,resize=False,resize_shape=(224,224)):
    '''
    Save Images to disc present at a URL. Images are saved in RGB  format.
    args:
        url: {str} URL string where image is present
        OUT_DIR: {str} Path to the output directory where you want to store the image
        img_name: {str} Name of image
        resize: {bool} Whether to resize image or not. {default: True}
        resize_shape: {tuple} shape of the resize image {default: (224,224)}
    '''
    pass

我的URLS位于CSV文件中,函数一次输入一个URL,我正在将函数实现为

for i in range(start,start+end):
    if count%250==0:
        print(f"Total {total_images_already+count} images downloaded in directory. {end-count} remaining from the current defined\n")

    url = df.iloc[i,ind]
    try:
        downloader_function(url,DIR,str(i),resize=resize,resize_shape=resize_shape)
        count+=1

    except (KeyboardInterrupt,SystemExit):
        sys.exit("Forced exit prompted by User: Quitting....")

对于多处理,我有2个选项:

选项1

p1 = multiprocessing.Pool(10)
p1.map(downloader_function,list_of_urls)

选项2

ThreadPool(10).imap_unordered(downloader_function,list_of_urls)

第二,我制作了一个脚本,该脚本使用argparse库并接受用户的输入。一些参数是可选的,一些是必需的。那么如何在将args映射到函数的同时传递downloader_function的这些args?

例如,有时用户想要调整大小,有时则不想。而且他希望每次都会有不同的价值。

所以我通过了整个清单?在运行时创建动态元组,例如resize,shape等?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)