Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Waiting for Alarm

by n8ur (Acolyte)
on Feb 16, 2008 at 15:39 UTC ( [id://668314]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to implement a program that calls a function every X seconds. I have the basic timer stuff working, using the methods in Time::HiRes, but I'm stumped on a very simple point.

My alarm code works, but the only way I've found to keep the program running is to do something like a "while (true) {};" loop. My repeating alarm goes off and does what it's supposed to, but that tight loop uses up 99.8 percent of the CPU.

I can't use a sleep call in the loop, as that interacts with the alarm timing.

So, is there some sort of way to make my program just sit and wait, doing nothing, until the alarm fires?

Thanks, John

Replies are listed 'Best First'.
Re: Waiting for Alarm
by traveler (Parson) on Feb 16, 2008 at 18:49 UTC
    You can pause a perl script waiting for an alarm with:
    use POSIX qw(pause); $SIG(ALRM) = ... ... alarm $seconds; ... pause; #wait for a signal without #using CPU time
      Perfect! Thanks.

      John

Re: Waiting for Alarm
by NetWallah (Canon) on Feb 16, 2008 at 17:47 UTC
    alarm does wake the program in sleep, at least on my perl, v5.10.0 built for MSWin32-x86-multi-thread.

    Here is the code

    use strict; sub Call_with_Timeout{ my ($TIMEOUT, $CODEREF) = @_; eval { local $SIG{ALRM} = sub { die "alarm time out" }; alarm $TIMEOUT; $CODEREF->(); # Whatever needs to be done alarm 0; 1; # return value from eval on normalcy } or return -1; # Timeout happened return 1; # OK } my $t=Call_with_Timeout(3, sub{ sleep 3, print "$_.\n" for 1..2}); print "Timed out=$t;\n";
    prints
    Timed out=-1;
    Which means that the alarm woke it up.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

      When I tried using sleep in the loop, it bombed out after the first alarm -- I got one print from the handler, and then the program exited. However, I just tried again with this:

      #!/usr/bin/perl
      
      use Time::HiRes qw ( setitimer ITIMER_REAL time );
      my $count;
      
      $SIG{ALRM} = sub { print time, "\n" };
      setitimer(ITIMER_REAL, 1, 1);
      
      while (1) {
              sleep 1;
              }
      
      

      and it worked fine. If this is reliable, it does the job for me. But I wonder why the first time it only worked for the first iteration...

      Thanks!

        Ignore the "my $count" -- that's left over from an earlier experiment. Apart from the while loop, this is based on an example in the Time::HiRes documentation.

Re: Waiting for Alarm
by BrowserUk (Patriarch) on Feb 16, 2008 at 17:10 UTC

    alarm is useful to interupt other processing at a given time or regular intervals, but if you have nothing else to do but wait for the intervening period, then you are using the wrong tool. Forget alarm and use sleep.

    Do something every 10 seconds:

    my $deadline = time() + 10; while( 1 ) { sleep 1 while time() < $deadline; $deadline = time() +10; ## Do the something }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Using sleep (or usleep from Time::HiRes) is how I've done it in the past, but the problem is accumulating time inaccuracy do to delays in processing.

      For a bit more context, I'm taking measurements from electronic test equipment using the GPIB bus. I want to take those readings, say, every 10 seconds. The time it takes to tell the instrument to send a reading, for the instrument to process that request, and then return the result, is somewhat variable. So it's very hard to calibrate the sleep time to keep accuracy.

      What I want to do is trigger the measurement cycle every X seconds regardless of how long (less than X, obviously) the measurement process takes.

      In one previous iteration, I used HiRes gettime calls before and after the read/write cycle to determine how long the processing took, and used that to adjust the sleep time, but that seemed an awfully awkward way to go. I was hoping that a recurring alarm (which the HiRes setItimer function allows) would be a more elegant way to go.

      Thanks! Hope this helps clarify.

        What I want to do is trigger the measurement cycle every X seconds regardless of how long (less than X, obviously) the measurement process takes.

        That's why I used a while loop sleeping for 1 second each time to accumulate the 10 seconds, rather than a 10 second sleep:

        my $deadline = time() + 10; while( 1 ) { sleep 1 while time() < $deadline; $deadline = time() +10; ## Do the something }
        This way, no matter how long "Do something" takes, so long as it's less that 10 seconds, the next reading will be initiated on time. To within one second.

        Eg. If it takes 3 seconds to process, the while loop will iterate 7 times. If it takes 7 seconds, the loop will iterate 3 times only.

        If you need to get more accurate, then use Time::HiRes and sleep for 1/10 of a second in the while loop. It will be 10 times more accurate and still consume almost imeasurable cpu.

        Need more accurate still? Then sleep for 1/100th of a second. It will still consume very little cpu and be another order of magnitude more accurate.

        Beyond that, you start getting into the realms of how long it takes to read the clock, affecting your accuracy, but if you need accuracy beyond 1/1000th of a second, you should probably be using C or assembler.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        Look into the setitimer function in the POSIX module. It will give you a stream of signals at repeated intervals. This might be a real slick solution combined with the pause suggestion made by traveler.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-26 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found