首页 > 解决方案 > 尽管异常在 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()

标签: pythonpyqt5

解决方案


推荐阅读