1

I've been trying to optimize my application, and although I made the function run on average 10.06 seconds when I profiled it by itself, when it is put on a QThread, it takes around 17-22 seconds.

It's 2x slower in the QThread. How do I fix that?

The function is actually initializing a class called DocxDocument, which is a document that I read from a Word file and parsed it for my needs.

I have a QThread that creates this class and uses Qt signals to send progress information back to the GUI. Here is the code from that class:

class DocxImporterThread(QThread):
'''
This thread is used to import a .docx document, report its progress, and
then return the document that it parsed.
'''
reportProgress = pyqtSignal(int)
reportError = pyqtSignal(Exception, str)
reportText = pyqtSignal(str)

def __init__(self, filePath):
    QThread.__init__(self)
    self.setPriority(QThread.HighestPriority)
    self._filePath = filePath
    self._docx = None
    self._html = ''
    self._bookmarks = None
    self._pages = None
    self._stop = False

def run(self):

    def myProgressHook(percentage):
        self.reportProgress.emit(percentage)

    def myCancelHook():
        return self._stop

    try:
        self._docx = DocxDocument(self._filePath, myProgressHook, myCancelHook)

        if not self._stop:
            self._html = self._docx.getMainPage()
            self._bookmarks = self._docx.getHeadings()
            self._pages = self._docx.getPages()

    except Exception as ex2:
        print 'ERROR: The .docx document didn\'t import.'
        self.reportError.emit(ex2, traceback.format_exc())

The getMainPage(), getHeadings(), and getPages() are instantaneous because they just return a reference to something that the constructor already created. Here is the code I used to profile my DocxDocument class:

testFile = 'some_file.docx'
statSave = 'profile.out'

def progress(percent):
    print ' --', percent, '--'

cProfile.run('DocxDocument(testFile)', filename=statSave)
myStats = pstats.Stats(statSave)
myStats.sort_stats('cumulative', 'name')
myStats.print_stats()

Thanks for your time in looking at this!

Spen-ZAR
  • 818
  • 6
  • 19
  • [*Maybe this would help.*](http://stackoverflow.com/a/4299378/23771) – Mike Dunlavey Jul 18 '13 at 14:25
  • It looks like he cancels execution at random times and figures out what functions pop up the most in the stack trace. I'll try it out and report back. – Spen-ZAR Jul 18 '13 at 15:34
  • Did you get a solution to this @Spen-ZAR? Having similar issues – mfitzp Dec 03 '13 at 18:06
  • I haven't during all this time, yet. QThreads I think are still limited by the GIL. – Spen-ZAR Dec 13 '13 at 18:44
  • Did you ever figure this one out? I moved a function from a main QApplication into a QObject which then gets moved to a QThread, and also found it became roughly 2X slower. I found this SO question after searching google. This is Python 3.4, Qt, and PySide. – Broken Man Jul 17 '15 at 10:52

0 Answers0