【发布时间】:2014-02-28 05:45:17
【问题描述】:
我有互斥锁的问题,无法弄清楚为什么锁定和解锁之间的一段代码在所有线程中同时运行。 这是我的线程类:
class MyThread(QtCore.QThread):
mutex = QtCore.QMutex()
load_message_input = QtCore.pyqtSignal()
def __init__(self, id, window):
super(MyThread, self).__init__()
self.id = id
self.window = window
def run(self):
print "Thread %d is started" % self.id
self.get_captcha_value()
print "Thread %d is finishing" % self.id
def get_captcha_value(self):
MyThread.mutex.lock()
print "Thread %d locks mutex" % self.id
self.load_message_input.connect(self.window.show_input)
self.load_message_input.emit()
self.window.got_message.connect(self.print_message)
self.window.input_finished.wait(self.mutex)
print "Thread %d unlocks mutex" % self.id
MyThread.mutex.unlock()
@QtCore.pyqtSlot("QString")
def print_message(self, msg):
print "Thread %d: %s" % (self.id, msg)
我是这样描述窗口的:
class MyDialog(QtGui.QDialog):
got_message = QtCore.pyqtSignal("QString")
def __init__(self, *args, **kwargs):
super(MyDialog, self).__init__(*args, **kwargs)
self.last_message = None
self.setModal(True)
self.message_label = QtGui.QLabel(u"Message")
self.message_input = QtGui.QLineEdit()
self.dialog_buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
self.dialog_buttons.accepted.connect(self.accept)
self.dialog_buttons.rejected.connect(self.reject)
self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.message_label)
self.hbox.addWidget(self.message_input)
self.vbox = QtGui.QVBoxLayout()
self.vbox.addLayout(self.hbox)
self.vbox.addWidget(self.dialog_buttons)
self.setLayout(self.vbox)
self.input_finished = QtCore.QWaitCondition()
@QtCore.pyqtSlot()
def show_input(self):
print "showing input"
self.show()
self.setModal(True)
@QtCore.pyqtSlot()
def on_accepted(self):
print "emit: ", self.message_input.text()
self.got_message.emit(self.message_input.text())
self.input_finished.wakeAll()
这里是主线程:
import sys
app = QtGui.QApplication(sys.argv)
window = test_qdialog.MyDialog()
threads = []
for i in range(5):
thread = MyThread(i, window)
if not thread.isRunning():
thread.start()
threads.append(thread)
sys.exit(app.exec_())
输出如下所示:
Thread 0 is startedThread 1 is startedThread 4 is started
Thread 0 locks mutexThread 3 is started
Thread 2 is started
Thread 2 locks mutex
Thread 3 locks mutex
Thread 1 locks mutex
Thread 4 locks mutex
showing input
showing input
showing input
showing input
showing input
更新: 感谢 Yoann 的建议。 这是 MyThread 类代码现在的样子:
class MyThread(QtCore.QThread):
mutex = QtCore.QMutex()
load_message_input = QtCore.pyqtSignal()
def __init__(self, id, window):
super(MyThread, self).__init__()
self.id = id
self.window = window
# self.mutex = QtCore.QMutex()
self.load_message_input.connect(self.window.show_input)
def run(self):
print "Thread %d is started" % self.id
self.get_captcha_value()
print "Thread %d is finishing" % self.id
def get_captcha_value(self):
MyThread.mutex.lock()
print "Thread %d locks mutex" % self.id
self.load_message_input.emit()
mutex2 = QtCore.QMutex()
mutex2.lock()
self.window.got_message.connect(self.print_message)
self.window.input_finished.wait(mutex2)
mutex2.unlock()
self.window.got_message.disconnect(self.print_message)
print "Thread %d unlocks mutex" % self.id
MyThread.mutex.unlock()
@QtCore.pyqtSlot("QString")
def print_message(self, msg):
print "Thread %d: %s" % (self.id, msg)
现在我在第一个线程完成后得到这个异常:
Traceback (most recent call last):
File "/path/to/script/qdialog_threads.py", line 20, in run
self.get_captcha_value()
File "path/to/script/qdialog_threads.py", line 34, in get_captcha_value
MyThread.mutex.unlock()
AttributeError: 'NoneType' object has no attribute 'mutex'
【问题讨论】:
标签: python multithreading pyqt pyqt4