multithreading in bge?

with this could I do heavy operations and return the result when ready somehow?

I have a huge pathfinding grid I would like to rebuild occasionally

python’s GIL gets in the way of that, i have tried to get around it myself with little luck.

each sub proccess has it’s own gil
(this workaround prevents py from opening a blender window and forces proper multithreading)

the problem is do I return the data and let blender py know it’s ready. do I thread a*?

Pass data between processes in Python (using multiprocessing)

But if some process is going to take multiple frames, then a system executing coroutines or a state machine could spread the computation over several frames. Now of course executing in a parallel thread/process can speed up a bit, although we are writting Python…

The futures module makes this a lot easier, as you can just say ‘I want this function to be run threaded’ and then check if it’s done, and if it’s done get the result from the function call. I’ve used it when doing some heavy processing both in and out of the BGE.

Otherwise you have to manage synchronosation between the BGE python thread and the ‘worker’ thread. This can be quite complex, and if your system model doesn’t use queueing (producer consumer model) then you’re bound to end up in deadlock at some point.

I have had plenty of luck multithreading in BGE in Linux, but far less so in Windows. It seems that that stackexchange has at least some workaound for using it in Windows.

There’s a powerpoint online called ‘an introduction to python concurrency’ that I would highly suggest reading.

the problem is that multi treading in python runs on the same cpu core ,the multiprocess module is needed to spread it out over multiple cores.