从回调发出信号时 Python 应用程序崩溃
我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在发现崩溃的原因是 C++ 异常(在 phihag 的帮助下)后,我以这种方式更改了代码,并解决了问题:
如果工作正常,我有(输出):
如果出现错误:
所以它崩溃的原因是在第一次调用emit()时信号'sig'没有初始化。
我无法理解它是如何发生的,因为回调是从调用“parser.Start()”时启动的线程调用的,该线程在after连接信号“sig”时调用:
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:
In case of proper work I have (output):
And in case of Error:
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' :