CabbageDevelopment / qasync

Python library for using asyncio in Qt-based applications.
BSD 2-Clause "Simplified" License
307 stars 43 forks source link

Unable to use QMessageBox inside task. Cannot enter into task while another task is being executed. #82

Closed omelia-iliffe closed 9 months ago

omelia-iliffe commented 1 year ago

I am trying to use QMessageBox to prompt the user but it is cause other tasks I want to happen in the background to not start.

I've created a small code snippet that reproduces the error I am using a singleshot timer with a small interval to trigger the open_message_box task which than prevents the test task from starting.

How should I modify my code to function as expected. Message box opens and in background the test function is run

import sys
import functools
import asyncio
import qasync
from PyQt6.QtWidgets import QApplication, QMessageBox, QMainWindow
from PyQt6.QtCore import QTimer, pyqtSignal

class TestWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.show()
        self.timer = QTimer(self, timeout=self.open_message_box, singleShot=True, interval=1)
        self.timer.start()

    @qasync.asyncSlot()
    async def open_message_box(self):
        msg = QMessageBox().warning(self, "Warning", "This is a warning message", QMessageBox.StandardButton.Ok, QMessageBox.StandardButton.Cancel)
        if msg == QMessageBox.StandardButton.Ok:
            self.close()

async def test():
    await asyncio.sleep(1)
    print("test finished")
    return True

async def main():
    def close_future(future, loop):
        loop.call_later(10, future.cancel)
        future.cancel()

    loop = asyncio.get_event_loop()
    future = asyncio.Future()

    app = QApplication.instance()
    app.setWheelScrollLines(2)
    app.aboutToQuit.connect(
        functools.partial(close_future, future, loop)
    )

    window = TestWindow()
    await test()

    await future
    return True

if __name__ == "__main__":
    try:
        qasync.run(main())
    except asyncio.exceptions.CancelledError:
        sys.exit(0)

The error is:

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 "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
RuntimeError: Cannot enter into task <Task pending name='Task-1' coro=<main() running at /home/omeliailiffe/projects/jeff/motor-controller/tests/test_qasync.py:41> wait_for=<Future finished result=None> cb=[_QEventLoop.run_until_complete.<locals>.stop() at /home/omeliailiffe/.local/share/virtualenvs/motor-controller-GUYMidhA/lib/python3.10/site-packages/qasync/__init__.py:399]> while another task <Task pending name='Task-2' coro=<TestWindow.open_message_box() running at /home/omeliailiffe/projects/jeff/motor-controller/tests/test_qasync.py:17> cb=[asyncSlot.<locals>._error_handler() at /home/omeliailiffe/.local/share/virtualenvs/motor-controller-GUYMidhA/lib/python3.10/site-packages/qasync/__init__.py:781]> is being executed.
hosaka commented 9 months ago

Remove the qasync.asyncSlot() decorator from the timer callback, as well as the async keyword and it will work as you've described.

class TestWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.show()
        self.timer = QTimer(self, timeout=self.open_message_box, singleShot=True, interval=1)
        self.timer.start()

    def open_message_box(self):
        msg = QMessageBox().warning(self, "Warning", "This is a warning message", QMessageBox.StandardButton.Ok, QMessageBox.StandardButton.Cancel)
        if msg == QMessageBox.StandardButton.Ok:
            self.close()

If you need to have an async operation in the open_message_box, use the asyncSlot as you do currently:

class TestWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.show()
        self.timer = QTimer(self, timeout=self.open_message_box, singleShot=True, interval=1)
        self.timer.start()

    @qasync.asyncSlot()
    async def open_message_box(self):
        await asyncio.sleep(1)
        msg = QMessageBox().warning(self, "Warning", "This is a warning message", QMessageBox.StandardButton.Ok, QMessageBox.StandardButton.Cancel)
        if msg == QMessageBox.StandardButton.Ok:
            self.close()
omelia-iliffe commented 9 months ago

Although I appreciate your reply, your second suggestion will also produce the same error if the sleep is less than the sleep in test() I have learnt a lot more since posting this issue. This is how I would approach it now. Instead of relying on the QMessageBox().warning which is blocks the eventloop, I would create a QMessageBox in the init and attach signals to handle its result. The function that handles the results can be decorated with @qasync.asyncSlot(int).

class TestWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.show()
        self.message_box = QMessageBox()
        self.message_box.setText("This is a warning message")
        self.message_box.setWindowTitle("Warning")
        self.message_box.setIcon(QMessageBox.Icon.Warning)
        self.message_box.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
        self.message_box.finished.connect(self.message_box_results)
        self.message_box.show()

    @qasync.asyncSlot(int)
    async def message_box_results(self, result):
        print(QMessageBox.StandardButton(result))
        if result == QMessageBox.StandardButton.Ok:
            await another_async_function()
            self.close()
        else:
            self.close()