2014-09-01 53 views
-2

我通過geopy運行Nominatim Web服務,但由於使用策略或Internet連接,它經常失敗。如何處理髮生停止的失敗連接,並在幾秒或幾分鐘後重新運行代碼。該錯誤信息是:如何使用Nominatim處理錯誤?

GeocoderServiceError: <urlopen error [Errno 10060] A connection attempt failed because the 
connected party did not properly respond after a period of time, or established connection 
failed because connected host has failed to respond> 

GeocoderServiceError: HTTP Error 420: unused 

僞代碼將是這樣的:

try: 
    run web service 
except: 
    stop several seconds or minutes and rerun webservice at the same line and loops 
(if it fails again) 
    stop 30 minutes and rerun webservice 

任何暗示或建議將是最歡迎的。

謝謝!

+0

我認爲你應該弄清楚,爲什麼它經常跌倒。這不應該發生。 – kecer 2014-09-01 13:06:47

+0

儘量不要使用像'我想知道'的短語 - 而要問一個問題,比如'我怎樣才能' – Cullub 2014-09-01 13:06:47

+0

這是很膚淺的,但只是把這段代碼放在while循環中,另外嘗試/ except不忽略錯誤「它完全相反,你需要知道你想要捕捉哪個錯誤以及如何處理它,除非標記不是一個接一個,否則except標記將不會在相同的嘗試中工作兩次,但除了1或except2或....除N 。 – 2014-09-01 13:12:36

回答

0

感謝您的評論。修改嘗試/除了解決方案。

根據geopy文檔(http://geopy.readthedocs.org/en/latest/#exceptions),使用geopy最常見的例外是GeocoderServiceError。這裏是修改代碼來處理錯誤。

try: 
    run web service 
except geopy.exc.GeocoderServiceError as e: 
    if e.message == 'HTTP Error 420: unused': 
     time.sleep(1800) 
     run web service 
    elif e.message == '<urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>': 
     time.sleep(5) 
     run web service