HighBit has asked for the wisdom of the Perl Monks concerning the following question:

I apologize for the ridiculous question, but I'm hoping somebody as ridiculous as me might know an answer:

Is there any way to run Perl code in the Apache (1.3/prefork MPM) parent process *after* the whole initialization process? For example, is there a way to run Perl code in the Apache parent *just before* it forks off a new child?

To be clear, I know it's quite easy to run whatever Perl code you want during mod_perl initialization (while Apache is starting up, and before Apache begins serving requests). What I'm curious about is, is there any way to run Perl code in the parent *after* Apache has started up and has been serving requests?

I know this is a horrible idea, for several reasons, not least of which being that it would be quite easy to hang or crash Apache by doing this, but I'm curious to see if any Monks have explored this idea.

mod_perl has PerlChildInitHandler, which runs in the child just after it has been forked. This is probably the best place to do any of the crazy things I'm thinking of doing; but, I can think of some good reasons to also have a PerlParentJustBeforeForkHandler. Comments?

Replies are listed 'Best First'.
Re: running mod_perl code before the fork
by CountZero (Bishop) on Jun 01, 2009 at 13:13 UTC
    There is the post-config phase (PerlPostConfigHandler) but I thought mod_perl could use that only as from Apache 2.

    The post-config phase is special as it is likely to run twice.

    From the docs:

    PerlPostConfigHandler

    The post_config phase happens right after Apache has processed the configuration files, before any child processes were spawned (which happens at the child_init phase).

    This phase can be used for initializing things to be shared between all child processes. You can do the same in the startup file, but in the post_config phase you have an access to a complete configuration tree

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: running mod_perl code before the fork
by perrin (Chancellor) on Jun 01, 2009 at 13:39 UTC
    No, it is not possible. Why don't you want to use ChildInitHandler?