0

I have an example based on the below moveToThread method, however, it looks like my main thread is waiting for my dummy thread to finish before continuing on. This causes my GUI to freeze in the interim. What could be causing this? If I were to comment out the self.sqlThread, the Widget init processes instantly.

Reference: How to use QThread correctly in pyqt with moveToThread()?

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import threading
import sys
import Sybase

def logthread(caller):
    print('%-25s: %s, %s,' % (caller, threading.current_thread().name,
                              threading.current_thread().ident))

class testTool(QWidget):
    def __init__(self):
        super(testTool, self).__init__()
        self.verticalLayout = QtGui.QVBoxLayout()
        self.searchq = QtGui.QLineEdit()
        self.model = QtGui.QStandardItemModel()
        logthread('mainwin_before_thread.__init__')
        self.sqlThread()
        logthread('mainwin_after_thread.__init__')
        self.filter_proxy_model = QtGui.QSortFilterProxyModel()
        self.filter_proxy_model.setSourceModel(self.model)
        self.searchq.textChanged.connect(self.filter_proxy_model.setFilterRegExp)
        self.verticalLayout.addWidget(self.searchq)
        self.tableWidget = QListView()
        self.tableWidget.setModel(self.filter_proxy_model)
        self.verticalLayout.addWidget(self.tableWidget)
        self.setLayout(self.verticalLayout)
        logthread('mainwin_end.__init__')

    def sqlThread(self):
        self.my_thread = QThread()
        self.my_thread.start()
        self.my_worker = GenericWorker(self.sql)
        self.my_worker.moveToThread(self.my_thread)
        self.my_worker.start.emit()

    def sql(self):
        logthread('mainwin.sql')
        self.db = Sybase.connect('')
        self.c = self.db.cursor()
        #a long queryyy
        self.c.execute(
            '''''')
        self.list = self.c.fetchall()
        self.all_user = []
        for user in self.list:
            self.all_user.append(user[0])
        self.list = self.all_user
        for self.text in self.list:
            self.item = QtGui.QStandardItem(self.text)
            self.model.appendRow(self.item)
        logthread('mainwin.sql_end.__init__')

class GenericWorker(QObject):

    start = pyqtSignal()
    finished = pyqtSignal()

    def __init__(self, function, *args, **kwargs):
        super(GenericWorker, self).__init__()
        logthread('GenericWorker.__init__')
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.start.connect(self.run)

    @pyqtSlot()
    def run(self, *args, **kwargs):
        logthread('GenericWorker.run')
        self.function(*self.args, **self.kwargs)
        self.finished.emit()


app = QApplication(sys.argv)
ex = testTool()
ex.show()
sys.exit(app.exec_())

Result:

mainwin_before_thread.__init__: MainThread, 19824,
GenericWorker.__init__   : MainThread, 19824,
mainwin_after_thread.__init__: MainThread, 19824,
GenericWorker.run        : Dummy-1, 7020,
mainwin.sql              : Dummy-1, 7020,
*here is the delay that freezes the GUI init for 3-4 seconds while the sql query processes.. *
mainwin_end.__init__     : MainThread, 19824,
mainwin.sql_end.__init__ : Dummy-1, 7020,

Edited the code to be runnable after you input your own long sql query.

Ken
  • 61
  • 7
  • provide a [mcve]......... – eyllanesc Feb 21 '19 at 02:16
  • as I observe the method `sql` is executed in a second thread so there you should not modify any QStandardItem since it is part of the GUI – eyllanesc Feb 21 '19 at 03:17
  • even if i were to comment out `for self.text in self.list: self.item = QtGui.QStandardItem(self.text) self.model.appendRow(self.item)`, the delay remains and a blank listview would generate. This leads me to believe somehow the query return (just returning approximately 55 items in a list) itself is holding up everything? – Ken Feb 21 '19 at 03:21
  • Well, until then my analysis arrived. If you provide a [mcve] that I can run then maybe I could point out the problem. :-) – eyllanesc Feb 21 '19 at 03:27
  • How should I go about to provide an SQL connect and query execute example? – Ken Feb 21 '19 at 03:44
  • I need you to indicate for example the query, a table example, tell me how many rows you have, the types of the fields, etc. so that I can implement it and verify what you point out, and so I can diagnose where the problem is. Understand that for me the rest is fine so maybe this is the problem in the part that you do not show. – eyllanesc Feb 21 '19 at 03:48
  • Have you corrected what I indicated? If so, then update your MCVE – eyllanesc Feb 21 '19 at 03:50

0 Answers0