【发布时间】:2018-02-28 05:09:56
【问题描述】:
我正在尝试使用 Python 从 URL 列表中下载图像。为了加快处理速度,我使用了多处理库。
我面临的问题是脚本经常自行挂起/冻结,我不知道为什么。
这是我正在使用的代码
...
import multiprocessing as mp
def getImages(val):
#Dowload images
try:
url= # preprocess the url from the input val
local= #Filename Generation From Global Varables And Rand Stuffs...
urllib.request.urlretrieve(url,local)
print("DONE - " + url)
return 1
except Exception as e:
print("CAN'T DOWNLOAD - " + url )
return 0
if __name__ == '__main__':
files = "urls.txt"
lst = list(open(files))
lst = [l.replace("\n", "") for l in lst]
pool = mp.Pool(processes=4)
res = pool.map(getImages, lst)
print ("tempw")
它经常在列表中途卡住(它打印完成,或者无法下载到它已处理的列表的一半,但我不知道其余部分发生了什么)。有没有人遇到过这个问题?我已经搜索过类似的问题(例如这个link)但没有找到答案。
提前致谢
【问题讨论】:
标签: python multiprocessing urllib python-multiprocessing