Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run Python scripts persistently?

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?

like image 747
GeneQ Avatar asked Oct 04 '22 15:10

GeneQ


2 Answers

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).

like image 144
Tim Pietzcker Avatar answered Oct 07 '22 20:10

Tim Pietzcker


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/

like image 34
Damian Avatar answered Oct 07 '22 19:10

Damian