CabbageDevelopment / qasync

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

Event Loop Policy Control #76

Closed Th3Tr00p3r closed 1 year ago

Th3Tr00p3r commented 1 year ago

Hi, I am currently using this truly great package for making an application which features a PyQt5 GUI and an asynchronously connected postgreSQL database (using psycopg). I'm currently developing on windows, which means the default event loop policy is 'WindowsProactorEventLoopPolicy'. However, psycopg async connection requires a 'WindowsSelectorEventLoopPolicy' policy to work.

I managed to solve this by manually overriding the if os.name != "nt": condition in the init.py script. Now the async connection seems to work great.

It would be nice to have an optional choice of policy for cases like this. I guess I could try to make this into a pull request if the overall idea makes any sense. If there is a way to do this that I missed, please let me know...

cheers!

hosaka commented 1 year ago

Hi, thank you for reporting this. This is something that qasync should support. Currently the QEventLoop type is created based on that os.name check as you pointed out.

When you say you "override" the condition check, do you simply replace this line in init.py?

- QIOCPEventLoop = type("QIOCPEventLoop", (_QEventLoop, _ProactorEventLoop), {})
+ QIOCPEventLoop = type("QIOCPEventLoop", (_QEventLoop, _SelectorEventLoop), {})

Setting different policies should at least be possible with set_event_loop_policy().

hosaka commented 1 year ago

@Th3Tr00p3r you can also try creating your loop by using the QSelectorEventLoop directly.

loop = qasync.QSelectorEventLoop()
Th3Tr00p3r commented 1 year ago

@Th3Tr00p3r you can also try creating your loop by using the QSelectorEventLoop directly.

loop = qasync.QSelectorEventLoop()

This appears to completely solve the issue! Thank you so much!