Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
6.7.2
-
None
-
Python 3.12.3
Description
I'm unsure if this is a actual bug or just a misuse of QtAysncio.
In the example below, if the close button of the main window is pressed by the user before the coroutine main_window.loop() has finished, a RuntimeError: Event loop stopped before Future completed is raised.
If the loop runs to completion, then everything is fine and the window closes without error at the end of the loop.
import asyncio import sys import PySide6.QtAsyncio as QtAsyncio from PySide6.QtWidgets import ( QApplication, QLabel, QMainWindow, ) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.text = QLabel("") self.setCentralWidget(self.text) async def loop(self) -> None: for i in range(10): self.text.setText(f"Processing {i}/10.") await asyncio.sleep(1) if __name__ == "__main__": app = QApplication(sys.argv) main_window = MainWindow() main_window.show() QtAsyncio.run(main_window.loop(), keep_running=False)
When keep_running=True, no exception is raised when the close button is pressed, but then the window is not closed at the end of the loop.
Is there a way to properly handle the window closing in the middle of a long coroutine with keep_running=False?