Форум сайта python.su
Есть класс-наследник QThread в котором выполняется обработка данных. Сама обработка осуществляется путем запуска консольного приложения, для чего использую QProcess. Код выглядит примерно так:
class WorkThread( QThread ): def __init__( self, data ): QThread.__init__( self, QThread.currentThread() ) self.mutex = QMutex() self.stopMe = 0 self.interrupted = False self.data = data self.command = "command_name" self.arguments = QStringList() def run( self ): self.mutex.lock() self.stopMe = 0 self.mutex.unlock() for d in data: self.arguments.clear() # формируем командную строку self.arguments << QString( "-param1" ) ... self.process = QProcess() self.process.start( self.command, self.arguments, QIODevice.ReadOnly ) if not self.process.waitForFinished( -1 ): print "Failed to process" self.mutex.lock() s = self.stopMe self.mutex.unlock() if s == 1: self.interrupted = True break if not self.interrupted: self.processFinished.emit() else: self.processInterrupted.emit() def stop( self ): self.mutex.lock() self.stopMe = 1 self.mutex.unlock() QThread.wait( self )
Warning: QObject: Cannot create children for a parent that is in a different thread.В документации написано
(Parent is QProcess(0xbd22e98), parent's thread is QThread(0xa364f50), current thread is WorkThread(0xa4d7468)
The child of a QObject must always be created in the thread where the parent was created. This implies, among other things, that you should never pass the QThread object (this) as the parent of an object created in the thread (since the QThread object itself was created in another thread).Но у меня как раз родитель и не указывается, т.е в _init_ пишу так self.process = QProcess(). В чем еще может быть проблема, что почитать по этому вопросу?
Офлайн
Попробовал объявить self.process в init, а в run только вызывать start()
__init__
вызывается в главном потоке, а run
уже в другом, потому и ошибка.
Но мне не очень нравится создание нового экземпляра QProcess на каждой итерации цикла.
Можно в начале run создавать. Если бы не было waitForFinished(), вообще, логичным бы смотрелось создание экземпляра на каждый из одновременно работающих процессов.
Но у меня как раз родитель и не указывается, т.е в init пишу так self.process = QProcess()
Warning: QObject: Cannot create children for a parent that is in a different thread. (Parent is QProcess(0xbd22e98), parent's thread is QThread(0xa364f50), current thread is WorkThread(0xa4d7468)
Тут имеется ввиду, что QProcess пытается создать какие-то дочерние объекты (когда вызывается start и т.п.) но он находится в другом потоке, т.е. к родителю процесса это отношения не имеет.
Офлайн