3

This image below says python takes lot of time in user space. Is it possible to reduce this time at all ?

In the sense I will be running a script several 100 times. Is it possible to start python so that it takes time to initialize once and doesn't do it the subsequent time ??

enter image description here

5
  • Do not trust time with measurements of such small granularity. Commented Jun 26, 2011 at 18:24
  • Actually this is the smaller version of the actual problem. The actual problem takes 0.003 seconds in system space, but takes lot of time in user space. As seen in stackoverflow.com/questions/6485538/… Commented Jun 26, 2011 at 18:37
  • @Tim Cooper - thanks for the edit. Commented Jun 26, 2011 at 18:44
  • I want to run a python script several 100 times from a perl code. Python scirpt as such runs fast. But it takes lot of time in user space setting up etc., So, I want to reduce this time. Commented Jun 26, 2011 at 20:20
  • You can't. Python takes a long time to set up its environment. You're just going to have to live with it. If this is really a problem, a client-server type program with the perl program passing data to the python program via a socket might be an option. However, several hundred times isn't very many times. I suspect you're better off just running your program instead of writing a more complex solution. Commented Jun 26, 2011 at 22:19

3 Answers 3

3

I just searched for the same and found this:

http://blogs.gnome.org/johan/2007/01/18/introducing-python-launcher/

Python-launcher does not solve the problem directly, but it points into an interesting direction: If you create a small daemon which you can contact via the shell to fork a new instance, you might be able to get rid of your startup time.

For example get the python-launcher and socat¹ and do the following:

PYTHONPATH="../lib.linux-x86_64-2.7/" python python-launcher-daemon &
echo pass > 1
for i in {1..100}; do 
    echo 1 | socat STDIN UNIX-CONNECT:/tmp/python-launcher-daemon.socket & 
done

Todo: Adapt it to your program, remove the GTK stuff. Note the & at the end: Closing the socket connection seems to be slow.

The essential trick is to just create a server which opens a socket. Then it reads all the data from the socket. Once it has the data, it forks like the following:

        pid = os.fork()
        if pid:
            return

        signal.signal(signal.SIGPIPE, signal.SIG_DFL)
        signal.signal(signal.SIGCHLD, signal.SIG_DFL)

        glob = dict(__name__="__main__")
        print 'launching', program
        execfile(program, glob, glob)

        raise SystemExit

Running 100 programs that way took just 0.7 seconds for me.

You might have to switch from forking to just executing the code instead of forking if you want to be really fast.

(That’s what I also do with emacsclient… My emacs takes ~30s to start (due to excessive use of additional libraries I added), but emacsclient -c shows up almost instantly.)

¹: http://www.socat.org

Sign up to request clarification or add additional context in comments.

2 Comments

Alternatively you can check if you can use python without site file: python -S [script]. Without -S python -c '' takes 0.018s for me. With -S I am down to time python -S -c '' → 0.004s. Note that you might miss some installed packages that way.
I now posted this answer to my website, too: draketo.de/light/english/politics-and-free-software/…
1

Write the "do this several 100 times" logic in your Python script. Call it ONCE from that other language.

1 Comment

The perl code has a particular context at a point of time and calls the python code to get data for this context. So, that will require lots of effort rewriting the code. Hence looking for something simple.
0

Use timeit instead:

http://docs.python.org/library/timeit.html

1 Comment

timeit isn't going to help in this case. He isn't benchmarking a program, he's wondering if he can do anything about Python's slow startup time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.