Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where to call async method before handing control over to Qt event loop? #109

Open
manuel-wagesreither opened this issue Jan 2, 2024 · 0 comments
Labels
question Further information is requested

Comments

@manuel-wagesreither
Copy link

manuel-wagesreither commented Jan 2, 2024

Hello,

I hope the issues facility can also be used to ask questions.

The Readme currently lists the following qasync usage example:

import sys
import asyncio

from qasync import QEventLoop, QApplication
from PySide6.QtWidgets import QWidget, QVBoxLayout

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setLayout(QVBoxLayout())
        self.lbl_status = QLabel("Idle", self)
        self.layout().addWidget(self.lbl_status)

    @asyncClose
    async def closeEvent(self, event):
        pass

    @asyncSlot()
    async def onMyEvent(self):
        pass


if __name__ == "__main__":
    app = QApplication(sys.argv)

    event_loop = QEventLoop(app)
    asyncio.set_event_loop(event_loop)

    app_close_event = asyncio.Event()
    app.aboutToQuit.connect(app_close_event.set)

    main_window = MainWindow()
    main_window.show()

    with event_loop:
        event_loop.run_until_complete(app_close_event.wait())

If this examples MainWindow class would have an additonal async def do_something() function (decorated with @asyncSlot) , what would be the proper place to call it?

This wouldn't work obviously:

    [...]
    main_window = MainWindow()
    main_window.show()

    main_window.do_something() # can't call here, as we're not inside an async function.

    with event_loop:
        event_loop.run_until_complete(app_close_event.wait())

I'm a beginner to Python as well as Asyncio and Qt, so I'm a bit helpless here. I don't know where event_loop.run_until_complete() (or event_loop.run_forever() fwiw) start executing. Hence I'm unable to inject my code to run prior.

Thanks!

@hosaka hosaka added the question Further information is requested label Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants