Can asyncio be combined with PyQt5?

Charles peacech at gmail.com
Sun Oct 29 07:54:04 GMT 2023


It might be possible to run python-telegram-bot in a Qt worker thread and
then communicate using Qt signal/slot.

On Sun, Oct 29, 2023 at 2:36 PM ullix <ullix at urkam.de> wrote:

> Thank you, I was afraid of such an answer.
>
> Isn't it of convern that two major developments - PyQt and asyncio -
> exclude each other' use? In particular both of your links seem to suggest
> that a combination is possible.
>
> Rewriting the telegram-bot lib is not an option for me, and asyncio does
> not seem a stringent necessity, as its older version runs without it.
>
> Thanks again
>
> ullix
>
>
> Am 28.10.23 um 18:45 schrieb Charles:
>
> An async function is a coroutine and you would need to run it via Qt
> threads rather than the usual asyncio.
>
> For example you can use these libraries
>
> https://github.com/hanjinliu/qt-asyncio (simple)
> https://github.com/altendky/qtrio (more complicated)
>
> I don't think a constructor can be async though.
>
> If you just want the function to run asynchronously, why not just create a
> qt worker thread. Or if you want
> to use an async library, find if there is a library that is implemented
> over trio. A normal async library that runs
> over asyncio will not work with Qt.
>
> On Sat, Oct 28, 2023 at 10:55 PM ullix <ullix at urkam.de> wrote:
>
>> My PyQt5 app so far runs well. But trying to add functionality requiring
>> asyncio results in failure.
>>
>> I tried to integrate the telegram-bot function (pip:
>> python-telegram-bot). When using the old version 13.15 I got it running,
>> but I am swamped with deprecation warning. But the new version 20.6
>> mandates the use of asyncio, and here is where I lack understanding; I
>> can't get it to work.
>> This is working code of the old app:
>> ex = ggeiger()
>> ... class ggeiger(QMainWindow):
>> def __init__(self):
>> """init the ggeiger class"""
>> super().__init__()
>> Adding async and await all the way up to this point works, but with e.g.
>> this (and other attempts) it fails:
>> ex = await ggeiger()
>> ... class ggeiger(QMainWindow):
>> async def __init__(self):
>> """init the ggeiger class"""
>> await super().__init__()
>> I guess it is possible? Any pointers welcome?
>> ullix
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20231029/39c45554/attachment-0001.htm>


More information about the PyQt mailing list