如何在 OpenWrt 上加速 python 脚本(嵌入式 Linux)(缺少 pyc)
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 文件。
如果您不这样做,那么每次都会动态编译所有库,这是非常 cpu 蚂蚁时间密集型任务。
您的设备需要至少有 4MB 的可用空间 space,因为您正在 space 换取时间。
我的诀窍是在启动时创建检查,如果少于 150 个 pyc 文件,并且如果要从 py 编译 python 到 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 是为了获得全速我添加了 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 脚本!
在 OpenWrt 上可以 运行 Python 代码(确切地说是迷你 Python),但即使是一个简单的 "Hello World" Python 脚本也需要 6 -8 秒到 运行.
根据我的调查,所有 Python 模块都保存在 py 源代码中,并且在每个 运行.
上在内存中编译由于大约有 20 个或更多模块,而且 OpenWrt 运行 在小型嵌入式设备上运行,这会导致启动延迟,即使是最简单的 Python 脚本。
如何在OpenWrt上加速Python代码的执行?
为了将 Python 脚本加速 10 倍以上,可以选择预编译所有库并将它们写入 pyc 文件。
如果您不这样做,那么每次都会动态编译所有库,这是非常 cpu 蚂蚁时间密集型任务。
您的设备需要至少有 4MB 的可用空间 space,因为您正在 space 换取时间。
我的诀窍是在启动时创建检查,如果少于 150 个 pyc 文件,并且如果要从 py 编译 python 到 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 是为了获得全速我添加了 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 脚本!