POE is a good choice for the parent. It makes it easy to write event-driven code that handles state changes and timed events.
POE is single threaded, however, so you definitely need to fork() any workers that might block. There's some example start/stop code at Re: Perl Background processes in Windows. Remove the setsid() call from that and the children will stay in the parent process group and die when it does. You should also consider what you're doing with Stdin/Stdout/Stderr in the child processes - going to parent's tty, or to log file or /dev/null? Also if you interrupt with CTRL-C you probably need tidy up code.
Following is an example of using POE to control start/stop. You would need to make it more sophisticated to handle your rules for the list of parameters and the respawn intervals. E.g. stash the date/time of last exit against entries in your list and take account of that in the spawner.
#!/usr/bin/perl
use strict;
use warnings;
# POE checking/debug levels
sub POE::Kernel::ASSERT_DEFAULT { 0 } # DATA, EVENTS, FILES, RETVALS,
+USAGE
sub POE::Kernel::ASSERT_EVENTS { 1 }
sub POE::Kernel::ASSERT_USAGE { 1 }
sub POE::Session::ASSERT_STATES { 1 }
use POE;
use POSIX;
my $num_kids = 8;
my %kid_pids;
main();
END { do_end(); }
sub do_end
{
# FILL HERE
exit(0);
}
sub main
{
# catch CTRL-C interrupt to tidy up cleanly
$SIG{QUIT} = $SIG{INT} = $SIG{HUP} = \&do_end;
POE::Session->create
(
inline_states =>
{
_start => sub
{
print "Starting\n";
$_[KERNEL]->sig('INT', 'signal_handler');
$_[KERNEL]->delay(reaper => 1 );
$_[KERNEL]->delay(spawner => 1 );
},
_stop => sub {},
signal_handler => sub {
my ($kernel, $sig) = @_[KERNEL, ARG0];
print "caught SIG$sig\n";
do_end();
},
reaper => sub
{
$_[KERNEL]->delay(reaper => 1);
while ( (my $pid = waitpid(-1, POSIX::WNOHANG)) > -1 )
{
my $exit_status = $? / 256;
delete $kid_pids{$pid};
print "child $pid died status $exit_status\n";
}
},
spawner => sub
{
$_[KERNEL]->delay(spawner => 1);
# start new kid if slot free
if (scalar keys %kid_pids < $num_kids)
{
my $pid = start_child();
if ($pid > 0)
{
$kid_pids{$pid} = 1;
print "child $pid started\n";
}
}
},
},
);
$poe_kernel->run();
}
sub start_child
{
# FILL HERE
return $pid;
}
Regards, Peter
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.