PyQt核心应用程序在退出时不返回调用者

时间:2016-02-10 02:40:26

标签: python pyqt pyqt4

我有一个非GUI PyQt应用程序,我想执行一些清理任务并在退出事件循环后打印一条消息。

def main(name):
    signal( SIGTERM, onQuit )
    signal( SIGINT, onQuit )

    logging.basicConfig( level=LOG_LEVEL, format=LOG_FMT.format(name) )

    try:
        app = QtCore.QCoreApplication(sys.argv)
        QtDBus.QDBusConnection.sessionBus().registerService( "{}.{}.{}".format( DBUS_SERVICE,
                                                                                name,
                                                                                "CommandRelay" ) )
        relay = CommandRelay(name)
    except Exception:
        log.error( "Failed to create CommandRelay. process exiting." )
        raise
    try:
        app.exec_()
    except Exception:
        log.error( "Fatal exception occurred while running relay. Exiting." )
        raise
    finally:
        relay.destroy()
        log.info( "Exiting process now." )

此应用程序作为较大应用程序的子进程运行。收到SIGTERM信号后,它会退出应用程序。

def onQuit( signum, stackframe ):
    """ Handle terminate signal """
    try:
        log.info( "Terminate signal received." )
        QtCore.QCoreApplication.quit()
    except Exception:
        log.exception( "Exception occured while terminating" )
        sys.exit(1)
    sys.exit(0)

根据QCoreApplication.quit()的文档,程序应该在调用exec_()之后将控制权返回。但相反,它似乎立即退出。

平台是开放使用13.2 x86_64。

1 个答案:

答案 0 :(得分:0)

您的问题可能是Python在运行Python代码时处理信号 - 基本上类似于:

  • 发出信号了吗?
    • 如果是,请致电信号处理程序
  • 执行下一次执行

然而,当你的应用程序在Qt主循环中时,它在C ++代码中,所以Python从来没有机会对信号做出反应。

我知道有两个问题的解决方案:

轮询信号

使用QTimer

,只需几秒钟就可以执行一些Python代码
timer = QTimer()
timer.timeout.connect(lambda: None)
timer.start(1000)

缺点是它需要多达1秒才能对信号作出反应,并且你“毫无意义地”每秒运行一次代码 - 一直唤醒CPU可能是一个问题,例如:功耗也是如此,但我没有任何证据来支持这种说法,这更像是一种猜测。

然而,这是唯一适用于Windows的解决方案。

使用唤醒文件描述符

轮询信号

这会使用带有管道的QSocketNotifiersignal.set_wakeup_fd来监听信号。

结合方法

我在应用程序中处理信号的完整代码如下所示:

class SignalHandler(QObject):

    """Handler responsible for handling OS signals (SIGINT, SIGTERM, etc.).

    Attributes:
        _activated: Whether activate() was called.
        _notifier: A QSocketNotifier used for signals on Unix.
        _timer: A QTimer used to poll for signals on Windows.
        _orig_handlers: A {signal: handler} dict of original signal handlers.
        _orig_wakeup_fd: The original wakeup filedescriptor.
    """

    def __init__(self, *, app, quitter, parent=None):
        super().__init__(parent)
        self._notifier = None
        self._timer = usertypes.Timer(self, 'python_hacks')
        self._orig_handlers = {}
        self._activated = False
        self._orig_wakeup_fd = None

    def activate(self):
        """Set up signal handlers.

        On Windows this uses a QTimer to periodically hand control over to
        Python so it can handle signals.

        On Unix, it uses a QSocketNotifier with os.set_wakeup_fd to get
        notified.
        """
        self._orig_handlers[signal.SIGINT] = signal.signal(
            signal.SIGINT, self.interrupt)
        self._orig_handlers[signal.SIGTERM] = signal.signal(
            signal.SIGTERM, self.interrupt)

        if os.name == 'posix' and hasattr(signal, 'set_wakeup_fd'):
            # pylint: disable=import-error,no-member,useless-suppression
            import fcntl
            read_fd, write_fd = os.pipe()
            for fd in (read_fd, write_fd):
                flags = fcntl.fcntl(fd, fcntl.F_GETFL)
                fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
            self._notifier = QSocketNotifier(
                read_fd, QSocketNotifier.Read, self)
            self._notifier.activated.connect(self.handle_signal_wakeup)
            self._orig_wakeup_fd = signal.set_wakeup_fd(write_fd)
        else:
            self._timer.start(1000)
            self._timer.timeout.connect(lambda: None)
        self._activated = True

    def deactivate(self):
        """Deactivate all signal handlers."""
        if not self._activated:
            return
        if self._notifier is not None:
            self._notifier.setEnabled(False)
            rfd = self._notifier.socket()
            wfd = signal.set_wakeup_fd(self._orig_wakeup_fd)
            os.close(rfd)
            os.close(wfd)
        for sig, handler in self._orig_handlers.items():
            signal.signal(sig, handler)
        self._timer.stop()
        self._activated = False

    @pyqtSlot()
    def handle_signal_wakeup(self):
        """Handle a newly arrived signal.

        This gets called via self._notifier when there's a signal.

        Python will get control here, so the signal will get handled.
        """
        logging.debug("Handling signal wakeup!")
        self._notifier.setEnabled(False)
        read_fd = self._notifier.socket()
        try:
            os.read(read_fd, 1)
        except OSError:
            logging.exception("Failed to read wakeup fd.")
        self._notifier.setEnabled(True)

    def interrupt(self, signum, _frame):
        """Handler for signals to gracefully shutdown (SIGINT/SIGTERM)."""
        # [...]