http://qs321.pair.com?node_id=48943

dcardamo has asked for the wisdom of the Perl Monks concerning the following question: (programs and processes)

Is there a way already in place to make perl stay running as a system task so that it doesn't need to startup each time to interpret a program? Also, can it keep a stored copy of those programs in memory?
If not, is there anyone who wants to make this happen with me?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Can Perl remain persistent for scripts just like mod_perl does for apache?
by tye (Sage) on Dec 30, 2000 at 09:05 UTC

    I can certainly understand the desire for this. The start-up time for Perl scripts can be a bit of a problem (otherwise there would be no need for mod_perl -- though it is usually more of a problem when serving web pages than most other situations).

    Each time you run a script, the perl executable must start up, initialize a new interpreter, then compile the script and any modules or other stuff that the script requires. This can add up -- though, I think this is less often noticed as a problem on modern machines that have to be faster to deal with the growing software bloat.

    If perl is being run often, then most operating systems will keep the perl executable cached in memory (if not short on RAM). But the operating system can't cache the compiled Perl script no matter how often you run it.

    A lot of work has been put into a pre-compiler for Perl that takes a Perl script and compiles it to some other format that is much faster for Perl to load. Initially the load time for the pre-compiled scripts was actually longer than the original compile time because of the amount of data that had to be read in.

    I hear much improvement has been made in this area so you should check out the B (for Backend) modules and tools that come standard with modern versions of Perl.

    I've often wanted a good Perl shell that would let me do simple things like "rm file" without loading any external executables and would let me do more complex things by loading a module so that each time I want to do that again, it is already loaded for me. But that will only help start-up of some interactive commands.

    If you have just a few particular scripts that are being run over and over, then there are things you can do to speed those up, making all or much of the script into a demon that is always running, for example.

    tye (Nothing is obvious unless you are overlooking something)
Re: Can Perl remain persistent for scripts just like mod_perl does for apache?
by coreolyn (Parson) on Dec 30, 2000 at 08:25 UTC

    Possibly you are looking to daemonize your script?

    If so check out these modules at CPAN

Re: Can Perl remain persistent for scripts just like mod_perl does for apache?
by Anonymous Monk on Jan 03, 2001 at 01:19 UTC
    Yes, look at Speedy... http://daemoninc.com/SpeedyCGI/

    Although it refers to CGI, don't be misled into thinking that's all it can do. Sounds perfect for what you are asking for.

    Lindsay

Re: Can Perl remain persistent for scripts just like mod_perl does for apache?
by mp (Deacon) on Sep 16, 2002 at 19:52 UTC
    See PPerl.

    PPerl - Make perl scripts persistent in memory

Re: Can Perl remain persistent for scripts just like mod_perl does for apache?
by entropy (Sexton) on Jun 09, 2001 at 02:33 UTC
    If you want the perl compiler to load faster, you can do 'chmod o+t /usr/bin/perl' as the superuser. This will cause the text page to stay in swap (whatever that means... I quote the man page), and perl should load faster.
Re: Can Perl remain persistent for scripts just like mod_perl does for apache?
by ichimunki (Priest) on Dec 30, 2000 at 05:30 UTC
    What would be the advantage to this? The interpretation of the script is the slow part of start-up, if I'm not mistaken.

    If you want your script to stay in memory run the whole thing from inside a while (1) loop, with an explicit option to exit somewhere inside the loop.
      The /usr/bin/perl executable can take a second to start itself before it even begins to look at the script. That time would be saved.
      Also, mod_perl will keep the compiled version of the script in memory so that it doesn't need to be interpreted before running again. If you were to have a large script several thousand lines long, this would save time. Lastly, since perl is used so much, any time saved each time is lots of time saved overall!
        1) The only time the executable is not already in memory is when you type "perl script.pl". If I do a perl -e 'print "Hello World\n";' from my CLI it responds in an eyeblink. This leads me to believe that the only serious overhead concerns are related to interpretation... hence my suggestion that

        2) You can keep a script in memory by using a while loop, or you can set up a controller script that uses the other scripts you want precompiled and starts one of them up on command.

        3) Check the FAQ for more information on compiling scripts and reducing the size of your perl executable.
        If you're worried about performance this bad, consider turning your script into a daemon of sorts, either listening on a network socket, a unix domain socket or a fifo of some kind.
Re: Can Perl remain persistent for scripts just like mod_perl does for apache?
by extremely (Priest) on Dec 30, 2000 at 06:09 UTC
    Under most OS drive and memory caches keep perl in memory if it is called fairly regularly. Rather than explicitly preloading scripts let your OS cache the ones you use often.
Re: Can Perl remain persistent for scripts just like mod_perl does for apache?
by Anonymous Monk on Sep 16, 2002 at 19:31 UTC
    df

    Originally posted as a Categorized Answer.