0

I'm trying to get some pyqt thread pools and process working but have run into a few issues and was wondering if anyone could help. I've simplified the code down and have included it here. When you run the following it seems the started() method seems to get called twice for each process which isn't something I'd expect.

Also, I get the error QProcess: Destroyed while process is still running. Can anyone enlighten me as to why this occurs? Running startDetached() enables the code to work but because it doesn't return an object I lose all ability to use signals. Any help would be much appreciated. Thanks a lot

import sys
from PyQt4 import QtGui,QtCore

class Test(object):
    def __init__(self):
        self.elements = []
        for i in range(5):
            self.elements.append(Element(i))

    def run(self):                                                                                                 
        pool = QtCore.QThreadPool()                                                                                
        pool.setMaxThreadCount(1)

        for element in self.elements:
            pool.start(element.worker)                                                                        

        pool.waitForDone()


class Worker(QtCore.QRunnable):
    def __init__(self):                                                                                            
        super(Worker, self).__init__()                                                                             


class Element(object):                                                                                         

    def __init__(self, number):
        self.number = number

        self.worker = Worker()
        self.worker.run = self.run

    def run(self):
        process = QtCore.QProcess()
        def started():
            print "Started"

        process.started.connect(started)
        process.start('echo', 'hello')

app = QtGui.QApplication(sys.argv)

test = Test()
test.run()

widget = QtGui.QWidget()
widget.show()
app.exec_()
user3419537
  • 4,740
  • 2
  • 24
  • 42
Tim
  • 1
  • 1
    You are getting `QProcess: Destroyed` errors because `process` is deleted after the `run()` method returns. Make it a member of `Element` to avoid this. – user3419537 Oct 08 '14 at 09:50
  • Thanks a lot. That stops me getting the error but it now doesn't seem to hook up to started() at all. Any ideas? – Tim Oct 08 '14 at 10:28
  • Ok ... so using self.process.waitForFinished() allows the signals to work – Tim Oct 08 '14 at 11:27
  • I suspect that this happens for the same reason - `started()` is only defined while the program is inside of `run()`. Adding `self.process.waitForFinished()` will wait inside of `run()` until the process is finished. Is there any reason why you don't define `started()` as a member of the class also? – user3419537 Oct 08 '14 at 11:46

0 Answers0