[英]Python script in background downloading images, hangs up. How to keep downloading script running?
我正在数据收集服务器上的后台运行python脚本(downloader.py)。 它通过链接列表并下载每个图像。 (下面的代码段)由于有很多图像,因此我在后台运行“ nohup python downloader.py&”,没有挂断。
一切开始正常,但是经过任意数量的图像后,图像停止下载。 当我运行“ ps aux | grep downloader.py”时,进程PID仍显示为正在运行的进程,但是nohup.out输出文件刚刚打印了下一个文件的链接,如“ print(myfile +“代码段中的“ +链接)”。 (nohup.out中没有显示任何错误消息,它只是停止下载或继续处理链接文件中的下一个图像)。 可能是什么问题呢? UNIX中进程的“ nice”优先级设置为0。
for link in read_lines("data_links/"+myfile):
try:
print(myfile + " " + link)
counter = counter + 1
##downloading the image and saving the file in data_collection/
print("set file structure for download")
f = open("data_collection/" + myfile.replace("_links.txt", "") + "/" + str(counter) + ".jpg", 'wb')
print("beginning url request")
f.write(request.urlopen(link).read())
print("url request done, closing file")
f.close()
print("done downloading, moving onto next")
except:
print("downloading error, but no problem, we're moving on to the next one")
continue
您违反了一个基本原则-将您的try
/ except
代码尽可能具体。 现在,无论错误是什么,您都捕获每个异常,然后continue
处理,这是一个非常糟糕的主意。 将您的except
子句定制为可能发生的特定错误,然后对其进行适当处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.