H
H
Hukyl2020-06-01 20:48:17
Python
Hukyl, 2020-06-01 20:48:17

Python: How to exit a loop (asyncio)?

I need to understand the async(qt)-pyside code that has a server connection. There should be a flag that checks if you need to connect again. I don't understand how to make it, and if it can be done at all (I'm a bit of a novice in this area). Help please (for the third day I fight)

Here is the code:

(вид класса Chat есть чуть повыше в коде)

from PySide2.QtWidgets import *
from PySide2.QtWidgets import QMainWindow
import asyncio
from asyncio import transports
from asyncqt import QEventLoop

host = "127.0.0.1"
port = 4000

class ClientProtocol(asyncio.Protocol):
    transport: transports.Transport
    window: 'Chat'

    def __init__(self, chat):
        self.window = chat
        self.flag = False  # Сам флаг

    def data_received(self, data: bytes):
        decoded = data.decode()
        self.window.plainTextEdit.appendPlainText(decoded)        
        if decoded == "True":
            self.flag = True  

    def connection_made(self, transport: transports.Transport):
        self.transport = transport
        print("Conn made")

    def connection_lost(self, exception):
        print("Conn lost")


class Chat(QMainWindow, Ui_JustMessenger):
    protocol: ClientProtocol

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.send_message)
        self.setWindowTitle("JustMessenger")
        self.pushButton.setShortcut("Return")

    def send_message(self):
        message = self.lineEdit.text()
        self.lineEdit.clear()
        self.protocol.transport.write(message.encode())

    def create_protocol(self):
        self.protocol = ClientProtocol(self)
        return self.protocol
    
    """Проверка флага должна быть
    (по крайней мере мне так кажется)
    где-то здесь   |
                  \|/   """
    async def start(self):                                            
        self.show()                                                      
                                                                               
        loop = asyncio.get_running_loop()                  
                                                                               
        with open('ip_enter.txt') as file:                      
            ip = file.readline().split()                             
            coroutine = loop.create_connection(          
                self.create_protocol,                               
                host,                                                       
                port                                                         
            )                                                                  
                                                                               
        await asyncio.wait_for(coroutine, 1000)         


application = QApplication()
chat = Chat()
loop = QEventLoop(application)
asyncio.set_event_loop(loop)
loop.create_task(chat.start())
loop.run_forever()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question