Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2798

QtAsyncio: Exception when closing window and keep_running=False

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 6.7.2
    • QtAsyncio
    • None
    • Python 3.12.3
    • Windows

    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?

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            adherrma Adrian Herrmann (Inactive)
            damienb damienb
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes