在python上的运行时将数据从GUI传递到线程

时间:2019-06-20 09:10:17

标签: python multithreading pyqt pyqt5 python-3.6

我正在尝试构建一个小python应用程序,其中一个简单的Web服务器在后台运行,并且您可以使用GUI发送不同的消息。

我正在使用PyQt5和Python3.6,并且已经设法将数据从工作线程传递到GUI,但是我不知道该如何做。

这是我的代码的骨架:

MainWindow:

class Ui_MainWindow(object):
    def __init__(self):
        super().__init__()

        self.input = True
        # 1 - create Worker and Thread inside the Form
        self.obj = WebServer.WebServer(self.input)  # no parent!
        self.thread = QtCore.QThread()  # no parent!
        # 2 - Connect Worker`s Signals to Form method slots to post data.
        self.obj.dataReady.connect(self.onDataReady)
        # 3 - Move the Worker object to the Thread object
        self.obj.moveToThread(self.thread)
        # 4 - Connect Worker Signals to the Thread slots
        self.obj.finished.connect(self.thread.quit)
        # 5 - Connect Thread started signal to Worker operational slot method
        self.thread.started.connect(self.obj.startServer)
        # 6 - Start the thread
        self.thread.start()
        # 7 - Start the form
        self.setupUi()

    def setupUi(self): 
        # Set up the GUI
        #...

        self.MainWindow.show()

    def onDataReady(self, data):
        # Data received from working thread
        # Do stuff...

    def on_click_set2(self):
        self.input = not self.input
        print(self.input)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui_MainWindow()
    sys.exit(app.exec_())

WebServer:

class WebServer(QObject):
    finished = pyqtSignal()
    dataReady = pyqtSignal(dict)

    def __init__(self, input):
        super().__init__()
        self.input = input

    @pyqtSlot()
    def startServer(self): # A slot takes no params
        # self.loop = asyncio.get_event_loop()
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)
        coro = asyncio.start_server(self.handle_update, '192.168.2.1', 8888, loop=self.loop)
        self.server = self.loop.run_until_complete(coro)

        # Serve requests until Ctrl+C is pressed
        print('Serving on {}'.format(self.server.sockets[0].getsockname()))
        try:
            self.loop.run_forever()
        except KeyboardInterrupt:
            pass

        self.finished.emit()

    async def handle_update(self, reader, writer):
        data = await reader.read(100)
        message = data.decode()
        addr = writer.get_extra_info('peername')
        print(f'Received: {message} from {addr}')

        if self.input:
            print("Input is True")
        else:
            print("Input is False")

        reply = self.input

        print(f'Send: {reply}')
        writer.write(str(reply).encode())
        await writer.drain()

        print("Close the client socket")
        writer.close()
        self.dataReady.emit(reply)

因此,例如,我想将输入传递给线程,并且如果我像上面那样(显然)喜欢输入,则输入始终保持初始值,并且当我按GUI上的按钮时,线程中的输入不会改变。

我该怎么做,每当我按下按钮时输入的值都会更新(因此在运行时将值从GUI传递给线程)?我认为这类似于从线程传递到GUI,因此从GUI发出信号并将其连接到胎面,但是我不知道如何从工作线程中找到对GUI的引用。

那么关于如何做到这一点的任何建议?当然,也欢迎任何其他有关应用程序/后台服务器解决方案的代码或方法的输入!感谢您的提前帮助!

更新:

也许不清楚我的问题是什么,所以在这里:

当两个线程都并行运行时,如何从GUI线程向工作线程发送一个值?(如果上述代码对您有帮助,请使用它作为示例,否则一般示例将不胜感激)

1 个答案:

答案 0 :(得分:3)

不必将WebServer驻留在另一个线程中,仅需要在另一个线程中执行eventloop。在这种情况下,WebServer通过队列与服务器线程交换数据。尽管QObject不是线程安全的,但信号是安全的,但从QObject所在的线程之外的其他线程发出信号也没有问题

import asyncio
import threading
import queue
from functools import partial
from PyQt5 import QtCore, QtGui, QtWidgets


class WebServer(QtCore.QObject):
    dataReady = QtCore.pyqtSignal(object)

    def startServer(self):
        self.m_loop = asyncio.new_event_loop()
        self.m_queue = queue.Queue()
        asyncio.set_event_loop(self.m_loop)
        coro = asyncio.start_server(
            self.handle_update, "127.0.0.1", 10000, loop=self.m_loop
        )
        self.server = self.m_loop.run_until_complete(coro)
        print("Serving on {}".format(self.server.sockets[0].getsockname()))
        threading.Thread(target=self.m_loop.run_forever, daemon=True).start()

    @QtCore.pyqtSlot(object)
    def setData(self, data):
        if hasattr(self, "m_queue"):
            self.m_queue.put(data)

    def stop(self):
        if hasattr(self, "m_loop"):
            self.m_loop.stop()

    async def handle_update(self, reader, writer):
        reply = ""
        data = await reader.read(100)
        message = data.decode()
        addr = writer.get_extra_info("peername")
        print(f"Received: {message} from {addr}")
        if not self.m_queue.empty():
            data = self.m_queue.get(block=False)
            reply = data
        print(f"Send: {reply}")
        writer.write(str(reply).encode())
        await writer.drain()

        print("Close the client socket")
        writer.close()
        self.dataReady.emit(reply)


class Widget(QtWidgets.QWidget):
    dataChanged = QtCore.pyqtSignal(object)

    def __init__(self, parent=None):
        super().__init__(parent)

        self.m_lineedit = QtWidgets.QLineEdit()
        button = QtWidgets.QPushButton("Send", clicked=self.onClicked)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.m_lineedit)
        lay.addWidget(button)

        self.m_web = WebServer()
        self.m_web.startServer()
        self.dataChanged.connect(self.m_web.setData)

    @QtCore.pyqtSlot()
    def onClicked(self):
        text = self.m_lineedit.text()
        self.dataChanged.emit(text)

    @QtCore.pyqtSlot(object)
    def onDataReady(self, data):
        print(data)

    def closeEvent(self, event):
        self.m_web.stop()
        super().closeEvent(event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = Widget()
    w.show()
    sys.exit(app.exec_())