如何解决并行快速下载图像的更好选择-> 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 举报,一经查实,本站将立刻删除。