Identifying why a Python Qt program pauses when not visible on macOS

Viewed 51

I have a Qt Python program that logs data over a serial port. I'd like this program to always log data while running even when the application is not visible. Currently, when the application is not visible, the logging will pause after about ~45 seconds. Once the application window becomes visible again, logging resumes. The logging portion of the code is in a second thread using QRunnable and QThreadPool.

I've tried searching for the cause (or solution), but have not had much luck. Part of my problem is that I'm not sure if this issue is related to the OS, IDE, language, etc.

High-level details:

  • OS: macOS 12.4
  • IDE: vscode
  • Language/frameworks: Python3 / Qt (pyside6)

Does anyone have any ideas on why this application/thread might be pausing? Is it possible to have the application to continue to log data even when it is not visible? My hope is that once I'm pointed in the right direction I'll be able to address the issue.

UPDATE Example code

class LogSignals(QObject):
    result = Signal(dict)

class LogWorker(QRunnable):
    def __init__():
        super().__init__()
        self.signals = LogSignals()

    def run(self):
        try:
            for i in range(N_LOG_SAMPLES):
                result = self.getSerialData()
                self.signals.result.emit(result)

        except:
            traceback.print_exc()
            exctype, value = sys.exc_info()[:2]

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.threadpool = QThreadPool()

    def startLog(self):
        log_worker = LogWorker()
        log_worker.signals.result.connect(self.updateLogData)
        self.threadpool.start(log_worker)

    @Slot()
    def updateLogData(self, result: dict):
        #save data

app = QApplication(sys.argv)

window = MainWindow()
window.show()
app.exec()
1 Answers

You would need to check if your application is active, and start or drop the logging accordingly. You can get this by using:

QWidget *QApplication::activeWindow()

This would return a nullptr if your application does not have an active window. So, you would write something like this:

if (application->activeWindow())
    ; // Start / Keep logging
else
    ; // Stop logging
Related