fancidev/qtinter

Asyncslot eager execution and qtinter.modal

Opened this issue · 0 comments

Hi!
Firstly, thanks for the awesome library!

I've noticed that asyncslot and modal might not work correctly all the time. Particularly, when modal is used in the eagerly executed part of an asyncslot.

Environment: Python 3.10.12 (Linux), PySide6 6.5.1.1, qtinter 0.11.0.
I use qtinter.using_asyncio_from_qt().

For example:

# slot is connected via asyncslot
async def on_settings_btn_clicked(self):
    dialog_coro = qtinter.modal(settings_dialog.exec)
    # this call will result in an exception
    result = await dialog_coro()

The exception:

Traceback (most recent call last):
  File "...", line 233, in on_settings_btn_clicked
    result = await dialog_coro()
  File ".../site-packages/qtinter/_modal.py", line 29, in modal_wrapper
    loop.exec_modal(modal_fn)
  File ".../site-packages/qtinter/_base_events.py", line 232, in exec_modal
    raise RuntimeError('QiBaseEventLoop.exec_modal() must be called '
RuntimeError: QiBaseEventLoop.exec_modal() must be called from a coroutine or callback

If I add await asyncio.sleep(0) just before the await of a modal, then everything works fine. Like that:

# slot is connected via asyncslot
async def on_settings_btn_clicked(self):
    dialog_coro = qtinter.modal(settings_dialog.exec)
    await asyncio.sleep(0)
    result = await dialog_coro()

Am I doing something wrong here? What is the best practice for something like this?