Sockets 在后台运行服务器,而应用程序执行其他操作

Sockets 在后台运行服务器,而应用程序执行其他操作,sockets,flask,socket.io,pyqt5,flask-socketio,Sockets,Flask,Socket.io,Pyqt5,Flask Socketio,我正在尝试制作一个桌面应用程序,作为运行服务器的触发器。应用程序(服务器)还将从客户端接收数据并将其保存在服务器的计算机中 我已经使用flask和socketio用python制作了服务器。客户也在工作。所以现在我要为服务器制作GUI,这样非IT人员就可以运行这个服务器 因此,我在runServer函数(单击“Run Server”时调用)中注意到,它确实工作了,并且运行了服务器。但是,由于服务器正在运行,我无法执行任何其他操作。所以我试着用打印语句进行测试 print('hel

我正在尝试制作一个桌面应用程序,作为运行服务器的触发器。应用程序(服务器)还将从客户端接收数据并将其保存在服务器的计算机中

我已经使用flask和socketio用python制作了服务器。客户也在工作。所以现在我要为服务器制作GUI,这样非IT人员就可以运行这个服务器

因此,我在runServer函数(单击“Run Server”时调用)中注意到,它确实工作了,并且运行了服务器。但是,由于服务器正在运行,我无法执行任何其他操作。所以我试着用打印语句进行测试

        print('hello')
        self.sio.run(self.app, host = '0.0.0.0', port = 8090) #running the server         
        print('hello again')
“你好”已经打印出来了,但“你好又来了”没有

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        self.app = Flask(__name__)
        self.sio = SocketIO(self.app)
        self.port = 8090

    def init_ui(self):
        self.runServerButton = QPushButton('Run Server')
        self.runServerButton.clicked.connect(self.runServer)

        self.sayHiButton = QPushButton('Say Hi')
        self.sayHiButton.clicked.connect(self.sayHi)

        self.status = QLabel('Server running\nPlease enter address below to client(s)')
        self.address = QLabel('ipaddress')

        vBox = QVBoxLayout()
        vBox.addWidget(self.runServerButton)
        vBox.addWidget(self.sayHiButton)
        vBox.addWidget(self.status)
        vBox.addWidget(self.address)
        self.status.hide()
        self.address.hide()
        vBox.addStretch()

        self.setLayout(vBox)
        self.setWindowTitle('SDS App')

        self.setGeometry(1000, 500, 200, 200)
        self.show()

    def runServer(self):
        ip = socket.gethostbyname(socket.gethostname())
        self.address.setText(ip)
        self.status.show()
        self.address.show()
        self.runServerButton.setEnabled(False)

        print('hello')
        self.sio.run(self.app, host = '0.0.0.0', port = 8090) #running the server         
        print('hello again')

    @sio.on('hey waddup')
    def on_waduup():
        print('i\'m fine bro')
        status.setText("I'm fine bro")

    def sayHi(self):
        print('Hi!')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    a_window = Window()
    sys.exit(app.exec_())
那么有没有办法在后台运行服务器呢?或者是否有后门解决方案,例如制作一个运行两个文件的可执行文件;服务器和gui(必须是另一个客户端)

谢谢

sio.run()调用被阻塞,它启动web服务器并且不返回。为了不阻塞GUI应用程序,您需要做的是在后台线程中进行此调用,这样按钮处理程序函数就不会被阻塞。应该是这样的:

def runServer(self):
    # ...
    print('hello')
    self.sio.start_background_task(self.sio.run, host='0.0.0.0', port=8090)
    print('hello again')
start\u background\u task()。为了不阻塞GUI应用程序,您需要做的是在后台线程中进行此调用,这样按钮处理程序函数就不会被阻塞。应该是这样的:

def runServer(self):
    # ...
    print('hello')
    self.sio.start_background_task(self.sio.run, host='0.0.0.0', port=8090)
    print('hello again')
start\u background\u task()
函数的文档: