Raspberry Pi Python (Kivy) 使用 sudo 非常慢

2022-01-15 00:00:00 python kivy sudo raspberry-pi

问题描述

我一直在使用 KivyPie OS(为 Kivy 开发预配置 Linux)在 Raspberry Pi 上编写 Kivy 图形程序.

I've been writing a Kivy graphical program on Raspberry Pi, with the KivyPie OS (Linux pre-configured for Kivy development).

由于某种原因,如果使用 sudo 启动,它的运行速度会非常慢.通常,运行python main.py",程序每秒运行大约 30 个周期.但是,如果我执行sudo python main.py",它会以每 5-10 秒 1 个周期的速度运行.

For some reason, it's running extremely slow if started with sudo. Normally, running "python main.py", the program runs at about 30 cycles per second. However, if I do "sudo python main.py", it runs as slowly as 1 cycle per 5-10 seconds.

我需要使用 sudo 来访问 Raspberry 的 GPIO.(除非我尝试其他方法,否则我会看到人们讨论).

I need to use sudo to access Raspberry's GPIO. (unless I try some other way to do it, that I see people discuss).

不过,我很感兴趣,使用 sudo 导致性能大幅下降的原因可能是什么?有没有可能解决这个问题?

I'm interested, though, what could be the cause of such a massive performance drop with sudo? And is it possible to work around that?

PS:在我的 PC (Linux) 上运行相同的程序,无论是否使用 sudo,似乎都不会导致此类问题.仅在覆盆子上.

PS: Running the same program on my PC (Linux) with and without sudo doesn't seem to cause such problem. Only on Raspberry.


解决方案

好吧,我认为这个问题已经解决了,即使还有一些问题.

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

以下是重点:

  • 速度变慢的原因是 Kivy 无法在sudo"下加载正确的视频驱动程序,而是使用软件渲染.
  • 我还没有弄清楚为什么驱动程序没有加载 sudo 或如何修复它.不过……
  • 使用 Pyinstaller 编译程序后,一切正常.可执行文件可以使用 sudo 启动,GPIO 正在工作,Kivy 加载了适当的驱动程序,一切都运行得很快,应该如此.

总结一下,一开始问题的原因已经找到了,目前还没有找到直接用Python启动程序的解决办法,但是用Pyinstaller编译程序解决了这个问题.(不过,这不是一种方便的调试方式.)

To sum it up, the reason of the initial problem has been found, no fix for launching the program directly with Python was yet found, but the problem was removed by compiling the program with Pyinstaller. (still, not a convenient way for debugging.)

相关文章