从回调发出信号时 Python 应用程序崩溃

发布于 2024-12-07 02:40:16 字数 864 浏览 3 评论 0原文

我将 python 应用程序中的 c++ 库与 QT 一起使用。它使用回调与 UI 进行通信。 我的图书馆是“ts”。问题是我的程序有时会崩溃。 据我了解,当调用 emit() 时它会崩溃,但这种情况很少发生。 我的程序的简化版本如下所示:

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(programDesc):
    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)


    ...

救命!

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(programDesc):
    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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2024-12-14 02:40:17

在发现崩溃的原因是 C++ 异常(在 phihag 的帮助下)后,我以这种方式更改了代码,并解决了问题:

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

如果工作正常,我有(输出):

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

如果出现错误:

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

所以它崩溃的原因是在第一次调用emit()时信号'sig'没有初始化。
我无法理解它是如何发生的,因为回调是从调用“parser.Start()”时启动的线程调用的,该线程在after连接信号“sig”时调用:

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

parser.Start()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文