python3 startup time - ideas?
i've just written my 1st Omega app. When I press a button, my python3 code starts up, does some magic, and spells out today's weather forecast through a speaker.
My challenge is python's startup time - it literally takes 30 seconds for it to load the libraries I need and begin work. Do you have any tricks to make it quicker? I know I could have it run as a daemon, but while I'm working on the code I would need some tool to apply changes without restarting the daemon.
Have you tried compiling to bytecode?
Tried to compile with py_compile - didn't help.
According to "python3 -v" output, most of the time is spent reading system libraries. Take this simple file that does nothing:
Execution takes literally 20 seconds on my Omega.
were you able to compile or something wrong happened?
If possible, would you be able to share the code using github or something else?
I have found that python 2 is faster than 3 on the omega. Have you tried using 2? Any reason you need 3?
@Vinicius-Batista i was able to compile my code, but it wasn't any faster on startup
@Samuel-Mathieson no real reason to use py3 - I was learning Python so I thought I should start with the most recent version.
The time it takes to start bare shell in python2/3 is in my case:
- 4 seconds for Python 2.7
- 5.5 seconds for Python 3.4
But importing urllib.request increases the python3 startup time to 22 secs. For python2, importing urllib does not have that much impact (4->5.5).
Hi @Michal-Rok .
Python is an interpreted language, so this is usually slow during initialisation. I just created a small script that reads response from a rest service(http get) and print it on the oled expansion. It takes about 4 seconds to initialise. I believe that it's partially due to omega's hardware limitations.
So, if initialisation time is something crucial for your project, I would suggest look for some alternatives - namely C and C++.
Like Vinicius said, if you have no real reason to use something like C and can get away with python, if it is not crucial to have a fast initialization time, I would go with Python. Also, the difference between python 2 and 3 is minimal. Really 3 is just making things a little more consistent. Mostly if you change your print "ok" to print("ok") that should do it. I would go with python 2.
One suggestion. I have found that it is faster to use os.system() for some things rather than their python equivalents. Perhaps you could look into that?