Python 脚本在后台下载图像,挂断了。如何保持下载脚本运行?

Python script in background downloading images, hangs up. How to keep downloading script running?

我正在数据收集服务器的后台 运行 设置 python 脚本 (downloader.py)。它遍历 link 的列表并下载每个图像。 (下面的代码片段)由于有很多图像,我 运行ning "nohup python downloader.py &" 不挂断并在后台。

一开始一切正常,但在任意数量的图像之后,图像停止下载。当我运行"ps aux | grep downloader.py"时,进程PID仍然显示为运行ning进程,但是nohup.out输出文件只是打印了下一个文件的link ,由代码片段中的 "print(myfile + " " + link)" 输出。 (nohup.out 中没有显示任何错误消息,它只是停止下载或继续处理 link 文件中的下一张图像)。可能是什么问题呢? "nice"进程在unix中的优先级设置为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 代码尽可能具体。现在,您正在捕获每一个异常,然后 continueing,不管错误是什么,这是一个非常糟糕的主意。根据可能发生的 特定 错误定制您的 except 子句,然后适当地处理它。