Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Using Alarm to Handle Timing

by chromatic (Archbishop)
on Jul 21, 2000 at 00:12 UTC ( #23481=perlquestion: print w/replies, xml ) Need Help??

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

As the Perl threads implementation is still undergoing renovation, I've considered other approaches for handling issues of concurrency.

In one of my programs, it would be very useful to have certain tasks executed at certain times. Low-latency and real-time responses aren't critical, but if they happen within a few seconds either way, I'm happy.

Here's an approach I'm testing. Things work fine with my test, but I'd like to know if there are any problems with my concept or execution. Alternately, if there's a better way to do this, feel free to let me know. (Using a separate process or cron is a possibility, but it hampers porting, which is important with this project).

#!/usr/bin/perl -w use strict; # we're abusing signals for this trick $SIG{ALRM} = \&wake_up; start(); sub start { # arbitrary number, could be a variable alarm(5); while (1) { # random time so the handler executes in different spots sleep int rand(3); print "Here's a line after a sleep.\n"; sleep int rand(3); print "Here's a line after another sleep.\n"; # don't interrupt this chunk my $hold = alarm(0); print "Suspending alarm with $hold second(s) left.\n"; sleep int rand(3); print "There should never be an alarm right before this.\n"; # interruptions okay now alarm($hold); sleep int rand(3); print "This is the last line of the loop.\n\n"; } } sub wake_up { # don't interrupt the interrupt (probably unnecessary) $SIG{ALRM} = 'ignore'; print "\t=> in alarm handler <=\n"; # reset alarm alarm(5); # reinstall handler $SIG{ALRM} = \&wake_up; }

Replies are listed 'Best First'.
RE: Using Alarm to Handle Timing
by awwaiid (Friar) on Jul 21, 2000 at 00:57 UTC

    It looks and works perfectly as far as I can tell. Its a bit hard-wired with that 5 seconds... but as a demo it works great :)

    Is it possible to say $SIG{ALRM} = \&wake_up(5) by chance?

    --awwaiid
      Is it possible to say $SIG{ALRM} = \&wake_up(5) by chance?
      No. But you can pass a parameter by creating an anonymous subroutine:
      $SIG{MUND} = sub { &wakeup(5) };

      -- Randal L. Schwartz, Perl hacker

      I'd considered making a global or package variable $timeout. It would be in scope for both the initialization and the signal handler.

      What I'm trying to do is manage my Jellybean objects better. Some of them need to flush their caches every few minutes while others ought to sync data to storage every now and then. Using an internal cron-like feature seems to be pretty elegant, but I don't want to segfault randomly if it's avoidable.

RE: Using Alarm to Handle Timing
by merlyn (Sage) on Jul 21, 2000 at 07:52 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2021-12-06 07:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (32 votes). Check out past polls.

    Notices?