QtAsyncIO conflict with QDialog.exec()
-
When I use QtAsyncIO, I meet this problem,the code:
# Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause from __future__ import annotations from PySide6.QtCore import Qt from PySide6.QtWidgets import (QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget, QMessageBox) import PySide6.QtAsyncio as QtAsyncio import asyncio import sys class MainWindow(QMainWindow): def __init__(self): super().__init__() widget = QWidget() self.setCentralWidget(widget) layout = QVBoxLayout(widget) self.text = QLabel("The answer is 42.") layout.addWidget(self.text, alignment=Qt.AlignmentFlag.AlignCenter) async_trigger = QPushButton(text="What is the question?") async_trigger.clicked.connect(lambda: asyncio.ensure_future(self.set_text())) layout.addWidget(async_trigger, alignment=Qt.AlignmentFlag.AlignCenter) async def set_text(self): await asyncio.sleep(1) asyncio.ensure_future(self.set_text_2()) # Dialog().exec() QMessageBox.information(self, "Info", "This is an async message box.") self.text.setText("What do you get if you multiply six by nine?") async def set_text_2(self): await asyncio.sleep(1) print("hello world") if __name__ == "__main__": app = QApplication(sys.argv) main_window = MainWindow() main_window.show() QtAsyncio.run(handle_sigint=True)
Create a new async task without await,then call QMessageBox.information() or QDialog.exec()
below exception will occur:Traceback (most recent call last):
File "E:\test.venv\lib\site-packages\PySide6\QtAsyncio\events.py", line 571, in <lambda>
self._schedule_event(self._timeout, lambda: self._cb())
File "E:\test.venv\lib\site-packages\PySide6\QtAsyncio\events.py", line 579, in _cb
self._callback(*self._args)
File "E:\test.venv\lib\site-packages\PySide6\QtAsyncio\tasks.py", line 98, in _step
asyncio._leave_task(self._loop, self) # type: ignore[arg-type]
RuntimeError: Leaving task Task 'QtTask' with state: Done with exception (RuntimeError("Cannot enter into task Task 'QtTask' with state: Pending while another task Task 'QtTask' with state: Pending is being executed.")) does not match the current task Task 'QtTask' with state: Pending.