[英]How to speed up python scripts on OpenWrt (embedded Linux) (missing pyc)
在OpenWrt上可以运行Python代码(确切地说是微型Python),但是即使是简单的“ Hello World” Python脚本也需要6到8秒的时间才能运行。
从我的调查中可以看出,所有Python模块都保存在py源代码中,并且在每次运行时都在内存中进行编译。
由于有大约20个或更多模块,并且OpenWrt在小型嵌入式设备上运行,因此即使启动最简单的Python脚本,也会导致延迟。
如何加快OpenWrt上Python代码的执行速度?
为了将Python脚本的运行速度提高10倍以上,可以选择预编译所有库并将其编写为pyc文件。
如果不这样做,则每次都动态编译所有库,这是非常耗时的任务。
您需要拥有至少4MB可用空间的设备,因为您需要在空间上进行权衡。
我的技巧是在启动时创建检查,以检查是否少于150个pyc文件,以及是否将python从py编译为pyc。
# count python pyc modules and generate if needed
pyc=`find / -name *.pyc | wc -l`
if [ $pyc -lt 150 ]; then
python -m compileall
fi
如果您仍然看到python执行缓慢,请检查某些python库是否不在某些子目录中。 例如python-serial就是这样,为了获得最快的速度,我在statup up脚本中添加了python-serial目录。
# count python pyc modules and generate if needed
pyc=`find / -name *.pyc | wc -l`
if [ $pyc -lt 400 ]; then
python -m compileall
python -m compileall /usr/lib/python2.7/site-packages/serial/*.py
python -m compileall /usr/lib/python2.7/site-packages/serial/tools/*.py
python -m compileall /usr/lib/python2.7/site-
packages/serial/urlhandler/*.py
fi
也就是说,在OpenWrt / Lede系统上享受超快的python脚本!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.