As I've mentioned in another thread, it would be nice if there was a way to speed up Python apps on the Z the way quickexec speeds up C++ apps. I started thinking about today and it took me 41 minutes to put together a very basic working version. I'll call it quickpython because it makes sense (and google yielded just 5 hits mostly related to The Quick Python Book).
Be warned: I'm a complete Python newbie and I just wanted a proof-of-concept, so it's a butt-ugly hack. We'll get to that later. First you need to copy the following code into a file and call it quickpython.py:
CODE
#!/usr/bin/env python
from os import remove
from time import sleep
COMMAND_FILE="quickpython.cmd"
while 1:
try:
sleep(1)
commandFile=open(COMMAND_FILE, "r")
command=commandFile.read().splitlines()[0]
commandFile.close()
remove(COMMAND_FILE)
if command=="exit":
break
else:
execfile(command, {})
except:
continue
from os import remove
from time import sleep
COMMAND_FILE="quickpython.cmd"
while 1:
try:
sleep(1)
commandFile=open(COMMAND_FILE, "r")
command=commandFile.read().splitlines()[0]
commandFile.close()
remove(COMMAND_FILE)
if command=="exit":
break
else:
execfile(command, {})
except:
continue
The above code plays the "daemon" and keeps the Python runtime loaded. Now we need a way to send it innocent Python apps to be executed :twisted:. It will be a trivial shell script called qp.sh:
CODE
echo $1 >> quickpython.cmd
Once you have both files in place in the same directory, open konsole in that directory (if you're not already there) and try this:
chmod a+x qp.sh
./quickpython.py &
Now the daemon is running and we can try to feed it a Python script. Let's say you have an app you want to speed up and it's launched via myapp.py. Simply type
./qp.sh myapp.py
and it should start up. You will notice no change the first time but if you exit your app and do it again there should be a major difference. In the case of my diary app, launch time went from about 10 seconds to under 1 second (on an SL-5500). The daemon takes up about 2MB of RAM and, last but not least, you can shut it down with
./qp.sh exit
As you can see, it is a butt-ugly hack indeed. The most outrageous shortcomings include:
[list]- namespace trouble - the apps leave residue after they've run; QApplication keeps complaining that there should be only one app instance :oops:
- your app will not receive any command-line arguments you might pass to it
- all exceptions are ignored
- everything must run in the same directory
- the daemon can only run one application at a time; the next one won't launch until the current one exits
- it uses the most primitive form of inter-process communication - a buffer file
[list]I know nothing about threads in Python or IPC in Linux (yet) but I'm posting this to provoke those who do. Things are ridiculously easy with Python and maybe we are not that far away from a nice quickpython_0.1.ipk. After the kinks are ironed out, it shouldn't be hard to tweak qp.sh so that it lazily launches the daemon and then we can use it to intercept /usr/bin/python and the whole thing will be completely transparent.
Any feedback appreciated,
z.