CabbageDevelopment / qasync

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

Should qasync.run() be used? #117

Open pe224 opened 8 months ago

pe224 commented 8 months ago

In e65c342 the qasync example usage in the README has been updated, along with aiohttp_fetch.py.

Before, it used qasync.run()

async def main():
    app = qasync.QApplication.instance()

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

    mainWindow = MainWindow()
    mainWindow.show()

    await app_close_event.wait()
    return True

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

now it explicitly instantiates/sets the event loop objects and uses run_until_complete()

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

    event_loop = qasync.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())

What was the reason for the change? Are there differences between the approaches and if yes, which one should be used? (I am using the qasync.run() pattern at the moment)