PyQT QThread似乎没有发出信号。我想用PyQt5构建我的代码的一部分,它将与服务器通信。我希望它在单独的线程中,因为我主要在图像上操作,所以发送它可能需要一些时间。我希望能够发送它,当它从服务器收到响应时,它应该发出一个信号,允许发送下一组数据。
以下代码减少到最低限度。
导入系统 进口时间
string sEmail = string.Empty;
foreach (ListItem item in myListItem.Items)
{
if (item.Selected)
{
sEmail += item.Value + "; ";
}
}
带有loop1()的代码的输出:
from PyQt5.Qt import QThread, QWidget, QApplication
from PyQt5.QtCore import pyqtSignal
def ask_server():
return b'some_message_received'
def send_to_server(data):
pass
class QTClient(QThread):
result_signal = pyqtSignal(bytes)
def __init__(self, *args, **kwargs):
QThread.__init__(self)
self._data = None
self._result = ' '
def set_data(self, data):
self._data = data
def set_result(self, result):
self._result = result
def receive_data(self):
result = ask_server()
print('receiving_data{}'.format(result))
self.result_signal.emit(result)
self._result = result
def send_data(self, data):
send_to_server(data)
self._data = None
def run(self):
broadcast = True
while broadcast:
if self._data is not None:
self.send_data(self._data)
self.receive_data()
if self._result is None:
self.receive_data()
class QtClientManager(QWidget):
data = pyqtSignal(bytes)
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.slam_client = QTClient()
self.data[bytes].connect(self.slam_client.set_data)
self._result = ''
self.slam_client.result_signal[bytes].connect(self.get_result)
self.thread_slam = QThread()
self.slam_client.moveToThread(self.thread_slam)
self.slam_client.start()
def add_image(self, data):
print('adding data {}'.format(data))
self.data.emit(data)
def get_result(self, result):
print('getting result data {}'.format(result))
self._result = result
def loop1():
for i in range(2):
qqq.add_image(b'sssss')
while qqq._result != b'some_message_received':
time.sleep(1)
def loop2():
for i in range(2):
qqq.add_image(b'sssss')
#while qqq._result != b'some_message_received':
time.sleep(1)
if __name__ == "__main__":
app = QApplication([])
qqq = QtClientManager()
loop1()
sys.exit(app.exec_())
(它被卡住了)
带有loop2()的代码的输出:
adding data b'sssss'
receiving_datab'some_message_received'
在我看来,从adding data b'sssss'
receiving_datab'some_message_received'
adding data b'sssss'
receiving_datab'some_message_received'
getting result data b'some_message_received'
getting result data b'some_message_received'
内部发出的信号在某种程度上被卡住了,而不是向QtClientManager类发送数据。怎么做才能阻止?