Hi,
I always have a problem with the bge that turns into 30fps while my cpu is used has 15% (this is the logic which food performance … ) .
After research, I realized that the bge uses one core of my cpu .
According to the web, I would have to allocate my scripts on multiple cores. But I can not find a tutorial for how to do .
I found this :
import threading
def azerty ():
for xx in range ( 10000) :
print ( xx)
The function starts when I leave the game ( in the python window) . How to make the function starts as soon as I call? And above all, is what I 'm on track or I 'm officially a noob ? thank you P
It might be a wild guess, but it sounds your game’s design is simply not sufficient. Using threads will (most likely) just add more issues rather than solving any.
Due to Python’s global interpreter lock, using multiple threads generally will not let you take advantage of multiple CPU cores. If you’re doing a lot of number crunching, you may want to look into using numpy which can use multiple cores when doing certain operations (read more here).
Besides that, you main option would be to use the multiprocessing module. While a bit tricky to get working, each separate Python process can use a different processor core simultaneously. However, each process cannot share memory and won’t be able to directly access any game engine data.
You really should not need multiple cores for your processing unless it’s really REALLY heavy database manipulation or advanced maths.
You could probably make everything run just fine on one thread by removing unnecessary loops and cleaning up searching code. As Mobious said, multiple threads won’t use more cores anyway. You’d need to have multiple processes, which means you’re up for learning how to communicate by sockets.