【发布时间】:2023-09-11 05:21:01
【问题描述】:
根据我对扭曲的理解,反应器线程中运行的任何内容都不应阻塞。所有阻塞活动都应该委托给其他线程,以便在完成后将回调触发回反应器线程。
那么这也适用于 gtk 的东西吗?例如,如果连接...失败,我想显示“连接失败”消息。我会这样做吗:
def connectionFailed(self, reason):
dlg = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
buttons=gtk.BUTTONS_CLOSE,
message_format="Could not connect to server:\n%s" % (
reason.getErrorMessage()))
dlg.run()
或:
def connectionFailed(self, reason):
dlg = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
buttons=gtk.BUTTONS_CLOSE,
message_format="Could not connect to server:\n%s" % (
reason.getErrorMessage()))
reactor.callInThread(dlg.run)
或:
def connectionFailed(self, reason):
def bloogedy():
dlg = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
buttons=gtk.BUTTONS_CLOSE,
message_format="Could not connect to server:\n%s" % (
reason.getErrorMessage()))
dlg.run()
reactor.callInThread(bloogedy)
?
编辑:哦,好吧,后两者真的搞砸了。所以我想答案是第一个。那么我的问题是:为什么?看起来这会阻塞反应器线程。
【问题讨论】:
标签: python multithreading gtk twisted pygtk