Raspberry Pi Python (Kivy) extremely slow with sudo

I am writing a Kivy graphics program on a Raspberry Pi with KivyPie OS (Linux is preconfigured for Kivy development).

For some reason, it runs very slowly if it is started using sudo. Usually, by running "python main.py", the program runs at a speed of about 30 cycles per second. However, if I do "sudo python main.py", it runs as slowly as 1 loop in 5-10 seconds.

I need to use sudo to access the Raspberry GPIO. (unless I try another way to do this, I see people discussing).

I am wondering, however, what could be causing such a huge drop in performance with sudo? And is it possible to get around this?

PS: Running the same program on my PC (Linux) with and without sudo does not seem to cause such a problem. Only on raspberries.

+6
source share
2 answers

Well, I would call this problem resolved, even if a few questions remain.

Here are the key points:

  • The slowdown is caused by the fact that Kivy cannot load the proper video driver under "sudo" and instead use software rendering.
  • I did not understand why the driver does not load using sudo or how to fix it. But...
  • After compiling the program using Pyinstaller, everything works fine. The executable file can be launched using sudo, GPIO works, Kivy loads the appropriate driver, everything works quickly, as it should be.

Summing up, it turned out that the cause of the original problem was not found, a fix for launching the program directly from Python has not yet been found, but the problem was fixed by compiling the program using Pyinstaller. (still, this is not a convenient way to debug.)

0
source

The problem is that Kivy uses an alternate config.ini file for the root user, and not the one you have in ~/.kivy/config.ini .

At the top of your python file, you can add the following to make it use egl_rpi :

 import os os.environ['KIVY_WINDOW'] = 'egl_rpi' 

Alternatively, you can copy the ini file to the root directory using:

sudo cp ~/.kivy/config.ini /root/.kivy/config.ini

+1
source

All Articles