Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

More than one alarm-call at a time

by htoug (Deacon)
on Jan 16, 2004 at 14:02 UTC ( [id://321784]=perlquestion: print w/replies, xml ) Need Help??

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

I have often had the need to have more than one pending alarm call, but haven't found a (simple) way to do it.

The current use is for an online system, that communicates with one or more of a number of external servers. It is quite expensive to obtain a connection to the server, so it is preferable to do it as seldom as possible (Data::Lazy is a great help there), and the connection should be dropped again (after a reasonable delay) to release ressources.
This is quite easy: when the connection is established set a timer to discconnect after a certain time. At every activity on the connection extend the time before the timer is triggered.

This is quite simple, when you only have to work with one server, alarm does it nicely with a suitable subroutine.

Sys::AlarmCall can handle nested alarm calls (calls to Sys__AlarmCall::alarm_call can be nested and it will call any alarms that were pending when alarm_call was called).

I envision a module that overrides alarm, and "just" ensures that all registered alarms are called at the correct time. The user interface of alarm shouldn't need to change.

But I have been unable to find a module, that can handle overlapping alarm calls, and before I run out and do it; I would like to either find a module that does it for me or gather any good ideas that the monks have.

Replies are listed 'Best First'.
Re: More than one alarm-call at a time
by Roger (Parson) on Jan 16, 2004 at 14:44 UTC
    Are you working on a *nix / BSD platform? If so, there should be a "virtual alarm" signal called VTALRM (SIGVTALRM), implemented at the operating system level, designed to solve the limitation of one alarm per process. There can be multiple VTALRM signals pending for a single process.

    There is a perl module called Time::HiRes which has Virtual Timer/Alarm support, and it should work on Windows platforms too I believe.

Re: More than one alarm-call at a time
by bluto (Curate) on Jan 16, 2004 at 17:33 UTC
    You could write a wrapper that remembers the list of alarms that need to trigger and then just run alarm() on the one that is the closest in the future. Something (untested) like ...

    { my @alarm_times; sub myalarm { my $new_alarm = shift; if (defined $new_alarm) { if ($new_alarm) { push @alarm_times, time + $new_alarm; @alarm_times = sort { $a <=> $b} @alarm_times; } else { @alarm_times = (); } } while (my $t = shift @alarm_times) { next if $t < time; alarm($t - time); return; } alarm(0); } ... myalarm(10); # add alarm 10 seconds in future myalarm(0); # cancel all alarms myalarm; # set up next alarm after one triggers

    Your alarm signal handler will have to reestablish the next alarm to run by calling this routine without arguments. Note: if this routine is being called when another alarm occurs, you are probably in trouble. Even if you put something like 'alarm(0)' at the beginning of the routine, you will probably miss alarms. For this reason and other strange issues related to signals (i.e. sporadic core dumps), if at all possible, you may want to consider redesigning this since you are pushing the limits on what alarms were intended for.

    bluto

Re: More than one alarm-call at a time
by Fletch (Bishop) on Jan 16, 2004 at 15:31 UTC

    Possibly more of a change than you're willing to make, but POE provides methods to deliver events at specified times or after a specified amount of time.

Re: More than one alarm-call at a time
by blue_cowdawg (Monsignor) on Jan 16, 2004 at 14:22 UTC

    Once upon a time I had a similar requirement. My implementation was kinda ugly, but it worked.

    What I did was spawn a child which did the alarm work. When it's alarm call timed out it sent a SIGUSRx signal to the parent. In a spool directory I had files that were used as a primitive IPC method that indicated what the signal was for.

    TIMTOWTDI though...


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.
Re: More than one alarm-call at a time
by stvn (Monsignor) on Jan 16, 2004 at 18:58 UTC
    htoug,

    I may be being nieve here, but why can't you just fork for each server? Then each process will have its own SIGALRM. Of course, you would probably need to do some IPC between the children and parent, but there are a number of very nice CPAN modules for that depending upon your IPC needs.

    There is always select too, but i dont know how your server connections are implemented, so that may not be relevant.

    I am intrigued by this problem though, can you provide any more details?

    -stvn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://321784]
Approved by blue_cowdawg
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-19 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found