2014-09-01 90 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