python - 尽管异常在 PyQt5 中处理,但程序在 ConnectionError 上终止
问题描述
在我的 prgram 中,我使用 requests 模块来调用 url。如果出现网络错误或任何其他问题,我正在处理错误。在下面的异常块中,使用计时器我在特定时间间隔后再次调用服务(url)。我试图在没有网络的情况下运行程序,请求的响应是
“HTTPSConnectionPool(host='com', port=443): 最大重试次数超出 url: /api//issubmitted/2020/3/ (由 NewConnectionError('引起: 无法建立新连接: [Errno 11004] getaddrinfo failed ',))"
但程序未能调用定时器。它只是退出,进程完成,退出代码为 1073741845。
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QTimer
import sys
class ShowScreen():
def call_api(self):
resp1 = CallServices.is_submitted(self)
if resp1['isLocked'] == False:
Main()
class CallServices():
def is_submitted(self):
url = '%s/issubmitted/%s/%d/%s' % (url_sub, year, month, username)
try:
response = requests.get(url)
except (requests.exceptions.RequestException,
requests.exceptions.ConnectionError) as e:
print("error")
api_logger.debug('DEBUG- IsSubmitted Response: %s', e)
timer = QTimer(QApplication.instance())
interval = 1000
timer.setInterval(interval)
timer.timeout.connect(lambda: ShowScreen.call_api())
timer.start()
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
class MainClass():
def start_timer(self):
app = QApplication(sys.argv)
Main()
sys.exit(app.exec_())
if __name__ == "__main__":
m = MainClass()
m.start_timer()
解决方案
推荐阅读
- jenkins - 如何使用 groovy 更改计划作业的构建显示名称
- c++ - 重新定义符号的算术是如何工作的?
- docker - 尝试构建 Centos8 rootfs 和 docker 映像似乎挂在所需的用户交互上,无法访问
- python-3.x - 如何在python图像操作中消除类型转换中的值错误
- javascript - 某些提交的 Google Analytics 事件跟踪失败
- python - while 循环中的 for 循环 - For 循环不会遍历所有项目
- json.net - 如何从 json.net 中的 JProperty 获取 json 属性的值?
- javascript - 如何从组件调用 API 数据
- regex - 如何将模式转化为多个字段名称
- perl - 遍历数据库结果