Dmitry
Dmitry

Reputation: 316

PyQt5 is QTimer running in separate thread and is it blocking?

I'm having an app which uses a database. I want to set a timer to launch a function which will modify the db periodically. But I want to be sure that it is blocking, so no read-write operations with db until this function would finish.

My QTimer is in the GUI thread, so as far as I understand, it's slot will block main thread. Am I right?

    class AppLauncher(QtWidgets.QMainWindow, AppWindow.Ui_MainWindow):
        def __init__(self, parent=None):
            super(AppLauncher, self).__init__(parent)
            self.setupUi(self)
            flags = QtCore.Qt.WindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
            self.setWindowFlags(flags)
            self.setWindowState(QtCore.Qt.WindowFullScreen)

            self.fooTimer = QTimer(self)
            self.fooTimer.timeout.connect(self.foo)

        def foo(self):
            pass

    def main():
        app = QApplication(sys.argv)
        form = AppLauncher()
        form.show()
        app.exec_()

    if __name__ == '__main__':
        main()

Upvotes: 0

Views: 231

Answers (1)

musicamante
musicamante

Reputation: 48231

QTimer is always running in the thread it was created and started, but that doesn't matter, as it wouldn't change the resulting behavior of the timeout connected functions even if it was executed in another thread.

What always matters is the thread in which the slot/function is, and as long as foo is a member of an instance that is in the same thread of any other function you want to "block", it will work as expected, preventing execution of anything else until it returns.

Upvotes: 1

Related Questions