DanielSpaniel has asked for the wisdom of the Perl Monks concerning the following question:
Versions: Perl 5.10, Apache 2.x, CentOS 6
(modified since originally posted, so that process A kicks off the scripts, rather than using an intermediary script to do it)I have a Perl script (process A) which kicks off several other Perl scripts. Process A is kicked off via http.
I am trying to get process A to wait for x seconds after having executed the other scripts, which may have finished, or they may not have done; I don’t really care – I just need to wait x seconds in process A, and then continue.
I’ve tried several ways of doing this, but can’t seem to get it working as I want. Partly this is probably because I don’t really understand signals etc, etc properly. I used to have a way of making it work in Apache 1.3, but Apache 2.x has changed how it handles such things I think.
So, code which I can get to work as a single process (purloined from StackOverflow), as a test, is below:
#!/usr/bin/perl use strict; use My_Functions; &print_myheader(); my $br=(-t)?"\n":'<br />'; use POSIX qw(:signal_h); my $sigset_new = POSIX::SigSet->new(); my $sigset_old = POSIX::SigSet->new(); sigprocmask(SIG_BLOCK, $sigset_new, $sigset_old); if ($sigset_old->ismember(SIGALRM)) { print "SIGALRM is being blocked!$br"; $sigset_new->addset(SIGALRM); sigprocmask(SIG_UNBLOCK, $sigset_new); } else { print "SIGALRM NOT being blocked$br"; } $SIG{ALRM} = sub {print scalar(localtime()), " ALARM, leaving$br"; sig +procmask(SIG_BLOCK, $sigset_new, $sigset_old); exit; }; alarm(5); print scalar(localtime()), " Starting sleep...$br"; sleep (10); print scalar(localtime()), " Exiting normally...$br";
This does as expected, and exits after five seconds.
However, I’m not sure if it’s because I am not implementing the code properly, but I just can’t seem to get it working, and I can get no http output until each of the kicked-off scripts has finished.
The relevant snippet of my code from process A looks like this:
[…. code …] my $sigset_new = POSIX::SigSet->new(); my $sigset_old = POSIX::SigSet->new(); sigprocmask(SIG_BLOCK, $sigset_new, $sigset_old); if ($sigset_old->ismember(SIGALRM)) { $sigset_new->addset(SIGALRM); sigprocmask(SIG_UNBLOCK, $sigset_new); } else { # do nothing } eval { $SIG{ALRM}=sub{ sigprocmask(SIG_BLOCK, $sigset_new, $sigset_old); exit; }; alarm ($timer); for (@mylist) { my $cmd= "perl process_$_.pl args"; # tried with trailing & too system($cmd); } alarm (0); }; [ … more code …]
Any assistance would be greatly appreciated! Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl / Apache 2 / Alarms
by Eliya (Vicar) on Dec 29, 2011 at 02:24 UTC | |
by DanielSpaniel (Scribe) on Dec 29, 2011 at 13:12 UTC | |
by DanielSpaniel (Scribe) on Dec 29, 2011 at 15:29 UTC | |
|
Re: Perl / Apache 2 / Alarms
by pileofrogs (Priest) on Dec 28, 2011 at 22:34 UTC | |
by Anonymous Monk on Dec 28, 2011 at 22:47 UTC | |
by DanielSpaniel (Scribe) on Dec 29, 2011 at 01:16 UTC |