On OpenWrt it is possible to run Python code (mini Python to be exact) but even a simple “Hello World” Python script takes 6-8 seconds to run.
From my investigations it seams that all Python modules are kept in py source code, and are compiles in memory on each run.
Due to being some 20 or more modules and that OpenWrt runs on small embedded devices this cause delay in starting even the simplest of Python scripts.
How to speed up execution of Python code on OpenWrt?
Advertisement
Answer
In order to speed up Python scripts by more than 10x there is an option to precompile all libraries and write them as pyc files.
If you don’t do this then all libraries are compiled each time dynamically and this is very cpu ant time intensive task.
You need to have device with at least 4MB of free space, because you are trading off space for time.
My trick is to create check on boot if there are less than 150 pyc files, and if there are to compile python from py to pyc.
# count python pyc modules and generate if needed pyc=`find / -name *.pyc | wc -l` if [ $pyc -lt 150 ]; then python -m compileall fi
If you still see slow python execution check if some of python libraries aren’t located in some sub directories. For example python-serial is so in order to get full speed I added python-serial directories to statup up script.
# 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
And that is is, enjoy blazing fast python script on OpenWrt/Lede systems!