QMessageBox.information not use await when run asynico task at task1, but it try to enter into task2 so that RuntimeError.
error messasge
任务1开始
任务2开始
任务1运行中
Exception in callback Task.task_wakeup(<Future finished result=None>)
handle: <Handle Task.task_wakeup(<Future finished result=None>)>
Traceback (most recent call last):
File "lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
RuntimeError: Cannot enter into task <Task pending name='Task-2' coro=<MyWindow.task2() running at demo.py:31> wait_for=<Future finished result=None>> while another task <Task pending name='Task-1' coro=<MyWindow.task1() running at demo.py:26>> is being executed.
python code
import asyncio
import sys
import qasync
from PySide6.QtWidgets import QMainWindow, QMessageBox, QPushButton, QApplication
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(400, 300)
self.show()
self.button = QPushButton("按钮", self)
self.button.clicked.connect(self.create_tasks)
self.button.show()
def create_tasks(self):
"""创建任务"""
asyncio.create_task(self.task1())
asyncio.create_task(self.task2())
async def task1(self):
print("任务1开始")
await asyncio.sleep(0)
print("任务1运行中")
QMessageBox.information(self, "提示", "任务1已经完成")
print("任务1完成")
async def task2(self):
print("任务2开始")
await asyncio.sleep(1)
print("任务2完成")
def closeEvent(self, event):
super().closeEvent(event)
for task in asyncio.all_tasks():
task.cancel()
if __name__ == '__main__':
app = QApplication(sys.argv)
loop = qasync.QEventLoop(app)
asyncio.set_event_loop(loop)
myWindow = MyWindow()
myWindow.show()
try:
with loop:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
myWindow.close()
sys.exit(0)
QMessageBox.information not use await when run asynico task at task1, but it try to enter into task2 so that RuntimeError.
error messasge
python code