开发者

Python app crashes when emit signal from callback

开发者 https://www.devze.com 2023-04-08 18:22 出处:网络
I use my c++ library from python application with QT. It uses callback to communicate with UI. My library is \'ts\'. The problem is that my program crashes sometimes.

I use my c++ library from python application with QT. It uses callback to communicate with UI. My library is 'ts'. The problem is that my program crashes sometimes. As I understood it crashes when call to emit(), but it happens rarely. The simplified version of my program looks like this:

import ts
...
class Parser(QObject):

    sig = Signal(ts.ProgramDescriptions)

    ts = ts.Ts()

    def init(self):
        self.ts.SetProgramChangeCB(SetProgramListCB)

    ...

class Ui_Dialog(QMainWindow):
    def __init__(self, pars):
        self.parser = pars
    ...

def SetProgramListCB(programDe开发者_Python百科sc):
    print "SetProgramListCB"
    ui.parser.sig.emit(programDesc)



def SetProgramList(programDesc):
    print "SetProgramList"


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    Dialog = QDialog()

    parser = Parser()
    parser.init()
    parser.sig.connect(SetProgramList, Qt.QueuedConnection)

    parser.Start()

    ui = Ui_Dialog(parser)


    ...

Help!


After finding out that the cause of crash is C++ exception (with help of phihag), I changed my code in this way, and that fixed the problem:

def SetProgramListCB(programDesc):        
    try:
        print ui.ravisParser.sig
        ui.ravisParser.sig.emit(programDesc)
    except:
        print "Error"

In case of proper work I have (output):

<PySide.QtCore.Signal object at 0x01DD88C0>
<PySide.QtCore.Signal object at 0x01DD88C0>
<PySide.QtCore.Signal object at 0x01DD88C0>
...

And in case of Error:

Error
<PySide.QtCore.Signal object at 0x01DD88C0>
<PySide.QtCore.Signal object at 0x01DD88C0>
<PySide.QtCore.Signal object at 0x01DD88C0>
...

So the reason why it crashed was that in first call to emit() signal 'sig' was not initialized. I can't understand how it could happen, because callback is called from thread which starts when call to 'parser.Start()' which called after connect signal 'sig' :

parser.sig.connect(SetProgramList, Qt.QueuedConnection)

parser.Start()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号