Run asyncio event loop embedded in thread

42 Views Asked by At

I have a Qt application which uses pybind to embed python plugins providing some kind of handlers. From what I have read online gluing the event loops is nearly impossible. Now I wonder if it is possible to run an asyncio event loop in a c++ background thread and call gather in other threads (main or others, the scripts in c++ are called threaded, while the GIl serialized them again).

I think the question boils down to the following: if the c++ thread runs an asyncio eventloop, does it hold the GIL or does it release it while it idles?

If it holds the GIL I'll end up in a deadlock.

If it releases the GIL in theory the threads in C++ could enter the python space and call gather. Which is nice.

Then again this throws the question if gather locks the GIL, which would defeat the purpose because other c++ threads would not be able to enter the Python space and effectively it would behave the same as if I would not have used asyncio at all.

1

There are 1 best solutions below

1
mugiseyebrows On

Gluing two event loops is hard (but doable) in python application, but you have c++ application, so you can spawn as many event loops in separate threads as you wish, without any problem.

You can instantiate python interpreter in another thread and run any syncronous or asyncronous code there. Gil would freeze interpreter thread, but not main (gui) thread.