A
A
Anton2019-04-16 18:19:05
Python
Anton, 2019-04-16 18:19:05

Why does asyncio freeze pyqt5?

There are two interface windows, the first one is shown when the script starts, the second one after the button is pressed. But also, after pressing the button, an asyncio connection is created that receives signals.
The problem is that as soon as the connection is established the whole pyqt5 interface hangs. What is the error and how to fix it?
Main program

import sys
from PyQt5 import QtWidgets, QtGui, QtWidgets
import loginform2, mainclient2
import tclient2


class ClientWindow(QtWidgets.QMainWindow, mainclient2.Ui_MainWindow):
    def __init__(self):
        super(ClientWindow, self).__init__()
        self.setupUi(self)
        self.setupWidget = QtWidgets.QWidget(self)
        self.config = loginform2.Ui_Form()
        self.config.setupUi(self.setupWidget)
        self.show()
        
        self.centralwidget.setHidden(True)
        self.config.pushButton.clicked.connect(self.start)
    
    def start(self):
        self.setupWidget.setHidden(True)
        self.centralwidget.setVisible(True)

        self.asynccode()


    def asynccode(self):

        import asyncio
        asyncio.run(tclient2.main('127.0.0.1', 8888))

        
if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    main_window = ClientWindow()
    sys.exit(app.exec())

Client code
import asyncio


class aclient():
    def __init__(self, host, port):
        self.host = host
        self.port = port


    async def tcp_echo_client(self):
        reader, writer = await asyncio.open_connection(self.host, self.port)
        name = 'anonymous'
        writer.write(name.encode())
        while True:
            await self.test(reader)


    async def test(self, reader):
        incoming_message = await reader.read(1024)
        print('incoming message', incoming_message.decode())


async def main(host, port):
    await aclient(host, port).tcp_echo_client()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-04-16
@TonyTrueTony

GUI events are handled by an infinite loop that starts when you call app.exec()it. It cannot be stopped or the application will hang. But eventloop is also an infinite loop that runs when you call asyncio.run(). As a result, the asyncio event loop stops Qt's event loop. There can be two ways out: either use Quamash to use one cycle for both asyncio and Qt, or run the asyncio cycle in a separate thread (QThread is better).

V
Vitaly Lazarev, 2019-04-16
@laviol

I would venture to suggest that this is due to the fact that it asyncio.run()does not run in a separate thread and intercepts the interpreter until the asyncio event loop completes its work (spoiler: it does not complete).
Perhaps this thread will help solve the problem:
https://stackoverflow.com/questions/26270681/can-a...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question