[英]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.