Skip to content

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

@omelia-iliffe

Description

@omelia-iliffe

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions