Linux All phyton script addons are slow on startup
#1
Hi experts,

with fresh Raspberry Pi OS Lite and Kodi, Python3-caching is not working properly with the Kodi interpreter. This causes script addons to react very slow on startup, especially on raspberry pi.

In the normal case, python fetches the pre-compiled *.pyc from the /usr/lib/python3.*/../_pycache__ for the python standard libraries. However, Kodi compiles python code with optimization level 1, the pre-compiled file *.opt-1.pyc does not exists by default. As python cannot write into those directories from a user session, it remains like this and Kodi need to compile the libraries every time the script starts. This costs some time.   

On powerful desktops, this is not an issue however on the raspberry it is very noticeable.

The two workarounds I found are granting access to the __pycache__ folders in the /usr/lib/python3.* tree or pre-compile all libraries with optimization level 1. But they remain workarounds. 

Question: is there a way to start or configure Kodi not to use optimization level 1 when running python code?
 
Code:
sudo find /usr/lib/python3* -name "__pycache__" -exec chmod -R 777 {} \;
or
sudo python -m compileall -o 1 /usr/lib/python3*
Reply
#2
Possibly due to this: https://github.com/xbmc/xbmc/blob/5dda24...n.cpp#L476

But I'm not sure what is supposed to happen when library files are in a location not writable by a normal user.
The "chmod 777" definitely looks wrong. I'm not sure whether the compileall option is okay.
Reply
#3
Thanks and yes 777 is to relax,  this is why I don't like it and this is why I prefer to pre-compile it. However this needs to be done once in a while and therefore also not the best option, but it works. It just creates the *.opt-1.pyc files aside the pre-compiled *.pyc files coming with pip and doing so boosts the startup of addons, especially those with lot of python libraries used. 
 
Code:
setenv("PYTHONOPTIMIZE", "1", 1);
Yes, looks like this is the issue, so the question is, why is this needed for the linux compile target and is there a possibility to change this?
Reply
#4
You could try rebuilding kodi with that line commented out and see if issue is resolved.
Build instructions in second post here
Reply
#5
I did compile Kodi 20 on my Linux PC and can confirm that removing that line will solve the problem.

I do not know whether this has side effects or not, however I would suggest to make this change for all users. Linux based installations on slow hardware will certainly benefit.
Reply

Logout Mark Read Team Forum Stats Members Help
All phyton script addons are slow on startup0