Cleaner async I/O syntax with Qt

Kyle Altendorf sda at fstab.net
Mon Dec 20 20:04:00 GMT 2021


You don't just get to start using `async`/`await`.  You have to have a proper event loop running your coroutines, such as asyncio, Twisted, or Trio.  Since you are working with Qt you need an event loop that can co-exist with the Qt one.

https://github.com/twisted/qt5reactor
https://qtrio.readthedocs.io/en/stable/
https://github.com/CabbageDevelopment/qasync

Sadly, I'm the author for QTrio and the most recently active maintainer for qt5reactor and I've got a new job not using neither so they need some love...  and the asyncio options get forked every year or two it seems.  But, they can all work with Qt5 at least, as far as I know.

Personally, I try to use Qt as a GUI library and other tooling for the rest.  If I were writing this personally, I would reach for QTrio and httpx I guess.

Cheers,
-kyle

On Mon, Dec 20, 2021, at 13:20, Patrick Stinson wrote:
> Hello,
> 
> I am using QNetworkAccessManager / QNetworkRequest for my async http requests. I am trying to find a way to wrap these in a cleaner python syntax. My first though was an async context manager, effectively like this:
> 
> with qt_request('https://google.com') as response:
>     # executed later
>     print(response.body)
> # executed now
> print("Sent request...")
> 
> I have been reading the docs on async context managers, but haven’t gotten the concepts into my head yet. Probably because I don’t fully get the async and await keywords yet either. Maybe this is actually counter to the python language spec?
> 
> Cheers,
> -Patrick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20211220/2991a5d7/attachment.htm>


More information about the PyQt mailing list