5

I'm new to Python and I wrote a few command line scripts to do some computations. In Perl I remember using PersistantPerl to speed up Perl scripts by running them persistently.

Think of it as something like FastCGI but for command line scripts. PersistantPerl always keep a copy of the interpreter running then background so there's so startup penalty each time the script is run.

Is there an equivalent tool in Python or are there other strategies to avoid paying the startup penalty for running the same Python script frequently.

PersistenPerl

UPDATE:

Someone seems to have stumbled upon with the same idea:

Reducing the Python startup time

But it looks more like a hack than a complete solution. Any modules out there that can do this?

GeneQ
  • 7,485
  • 6
  • 37
  • 53
  • Did you measure the "startup penalty"? Does it impact your work? –  Jul 03 '13 at 10:10
  • I've never heard of such a feature for the Python interpreter; probably the impact of this isn't big enough to justify the effort. Today's OSes keep the executable's binaries in memory anyway (unless you've haven't got enough RAM). – Alfe Jul 03 '13 at 10:16

2 Answers2

3

One thing that may make more of an impact than keeping the Python interpreter running (if that's even possible) is making sure that Python doesn't have to compile your script every time it runs it.

The simplest way to achieve that would be to have a small startup script that imports your actual script. Imported scripts are saved as precompiled .pyc files and can thus be re-run faster (as far as startup time is concerned).

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Sounds interesting. I'll give it a try. – GeneQ Jul 03 '13 at 10:30
  • @GeneQ Try [this question](http://stackoverflow.com/questions/12339671/how-to-compile-python-script-to-binary-executable) out. Please post the results. I would also like to know which is currently the best method. I was going to ask the same question. – Aseem Bansal Jul 03 '13 at 10:38
1

If this is really a big issue, you could convert your script to run as a daemon and/or webservice which gets invoked by a command line tool?

See:

https://pypi.python.org/pypi/python-daemon/

Damian
  • 2,588
  • 23
  • 19