python-3.x - WMS查询期间如何排除TimeOutError?
问题描述
我需要你的建议。我正在与 WMS(Web 地图服务)建立连接。如果已建立连接,则添加 WMS 以在 matplot 窗口中绘制。在大多数情况下,来自 WMS 的图像可以正确显示。但是,在某些情况下,来自 WMS 的图像加载时间过长,这会导致错误(我附在下面)。这通常是超时错误。
我想将我的代码变成一种形式,在这种形式中,当超过 TimeOut 时,来自 WMS 的图像停止加载。我想避免再次请求图像。
为此,我想要 TimeError 除外。不幸的是,我不能。当然,我试过
除了:
方法(没有指定错误),但它不起作用。下面的代码也不起作用。
try:
resp = requests.get('http://mapy.geoportal.gov.pl/wss/service/img/guest/ORTO/MapServer/WMSServer')
odpowiedz_wms = resp.status_code except:
resp.status_code = 0 if resp.status_code == 200:
wms = ax.add_wms(wms='http://mapy.geoportal.gov.pl/wss/service/img/guest/ORTO/MapServer/WMSServer', layers=['Raster'])
try:
plt.draw()
except (requests.exceptions.RequestException,socket.timeout,timeout, exceptions.ConnectionError,ReadTimeout, TimeoutError, exceptions.Timeout,exceptions.ReadTimeoutError,ReadTimeoutError,exceptions.ConnectTimeout, exceptions.ReadTimeout, requests.exceptions.ReadTimeout, urllib3.exceptions.ReadTimeoutError, socket.timeout) as e:
wms.remove()
plt.draw()
Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):(...)
文件“C:\Python35\lib\socket.py”,第 575 行,在 readinto 中返回 self._sock.recv_into(b) socket.timeout: timed out
在处理上述异常的过程中,又出现了一个异常:
回溯(最后一次调用):(...)文件“C:\Python35\lib\site-packages\urllib3\connectionpool.py”,第 306 行,在 _raise_timeout 中引发 ReadTimeoutError(self, url,“读取超时。 (读取超时=%s)" % timeout_value) urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='mapy.geoportal.gov.pl', port=80): 读取超时。(读取超时=30)
在处理上述异常的过程中,又出现了一个异常:
Traceback(最近一次调用最后一次):(...)r = adapter.send(request, **kwargs) File "C:\Python35\lib\site-packages\requests\adapters.py", line 526, in send raise ReadTimeout(e, request=request) requests.exceptions.ReadTimeout: HTTPConnectionPool(host='mapy.geoportal.gov.pl', port=80): 读取超时。(读取超时=30)
解决方案
当一段代码抛出异常时
- 堆栈将展开,直到它到达异常处理程序(try - except 块)
- 如果在处理异常期间引发另一个异常(在 except 块代码中)。然后堆栈将进一步展开到下一个能够捕获异常的异常处理程序。现在需要处理的异常不是原来的异常。现在是引发的最后一个异常
- 如果调用堆栈展开到没有异常处理程序进一步向上调用堆栈,则进程将终止并且堆栈跟踪将转储到您的屏幕上,显示所有错误
- 许多库包含记录器,它们将在处理异常期间打印出堆栈跟踪。在这些情况下,您仍然会将堆栈跟踪转储到您的屏幕上。但异常不一定会终止进程。可以处理。
- 在异常处理程序中实现日志记录是一种很好的做法,这样您就可以知道发生了哪些异常,以便在出现问题时进行调试
- 在这种情况下,您的代码必须处理的当前异常不是第一个抛出的异常。需要处理的是 ReadTimeout。虽然,使用不带参数的 except 是捕捉任何出现的可靠方法。这种异常处理方法必须谨慎使用,并且只能作为最后的手段。它通常会捕获您从未打算捕获的真正错误的异常。始终尝试指定您实际打算捕获的异常类型
例子
try:
try:
1/0
except ZeroDivisionError:
# Catches the ZeroDivisionError
raise IndexError
except IndexError:
# Catches the IndexError - Doesn't need to catch the ZeroDivisionError
raise NameError # raises unhandled exception. Process terminated and entire stack trace will be printed
输出
Traceback (most recent call last):
File "pyex.py", line 14, in <module>
1/0
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pyex.py", line 17, in <module>
raise IndexError
IndexError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pyex.py", line 21, in <module>
raise NameError # raises unhandled exception. Entire stack trace
will be printed out
NameError
推荐阅读
- android - Android Recycler 视图使用异步任务从图库中加载图像
- swift - 将 GIF 应用于 SCNPlane
- reactjs - 如何仅让所有者通过具有 Firebase 身份验证的登录页面进行身份验证
- node.js - 深 mongodb 聚合以在对象数组中创建具有公共字段的组?
- visual-studio-code - 如何找出哪个扩展在 vscode 中提供了命令?
- angular - 'com2' 等组件名称的 Angular CLI 组件生成错误
- reactjs - React Native 中的过滤问题
- shell - 想要下载可从主页访问的 html 文件列表
- javascript - 执行顺序——Promise 回调什么时候执行?
- c# - 使用本地数据库中的信息在使用 Xamarin 的 Android 中显示通知信息