# Timers and Alarms

**URL:** <https://www.ruby-forum.com/t/timers-and-alarms/129789>\
**Category:** Ruby\
**Created:** [February 21, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/timers-and-alarms/129789 "2008-02-21T16:20:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hans\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@Hans\_F](https://www.ruby-forum.com/u/Hans_F)\
**Post date:** [February 21, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/timers-and-alarms/129789/1 "2008-02-21T16:20:42Z")

</div>

I want a program to wait for some moment in time, and resume operation  
at that moment. sleep is ruled out because it just counts cycles and is  
thrown off when my laptop suspends.

In C I’d use a one-fire timer, using setitimer. But in some quick  
googling it looks like the ruby interpreter may use setitimer internally  
for thread scheduling. Is there a safe way in ruby to sleep until a real  
moment in time?

---

<div class="post-metadata">

**Author:** ![Hans\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@Hans\_F](https://www.ruby-forum.com/u/Hans_F)\
**Post date:** [February 21, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/timers-and-alarms/129789/2 "2008-02-21T16:46:09Z")

</div>

On Feb 21, 10:17 am, Hans F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want a program to wait for some moment in time, and resume operation  
> at that moment. sleep is ruled out because it just counts cycles and is  
> thrown off when my laptop suspends.

What about a roll-your-own solution like:

def my\_sleep(interval, granularity = 60)  
end\_time = Time.now + interval

remaining = end\_time - Time.now  
while granularity \< remaining  
sleep granularity  
remaining = end\_time - Time.now  
end

sleep remaining  
end

Even if you close your laptop, when you finally wake it, the timer  
will end within granularity seconds.

## Eric

Are you interested in on-site Ruby training that uses well-designed,  
real-world, hands-on exercises? [http://LearnRuby.com](http://LearnRuby.com)

---

<div class="post-metadata">

**Author:** ![Hans\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@Hans\_F](https://www.ruby-forum.com/u/Hans_F)\
**Post date:** [February 21, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/timers-and-alarms/129789/3 "2008-02-21T21:18:34Z")

</div>

On Feb 21, 12:57 pm, Hans Fugal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > remaining = end_time - Time.now
> > 
> > ```
> > 
> > end
> 
> > sleep remaining  
> > end
> 
> It’s going to have to be something like this I think. I don’t know how  
> much impact waking every so often (about once per second in my  
> situation) will have on power usage, but I see now that even setitimer  
> in C suffers the same problem sleep does with laptop suspend.

Looking at my code, it may be best to change the next-to-last line to:

sleep remaining if remaining \> 0

Which OS are you using? On OS X the man page for setitimer claims  
there are three separate timers, one of which uses real time. From  
the man page:

```
 The ITIMER_REAL timer decrements in real time. A SIGALRM signal

```

is  
delivered when this timer expires.

I don’t have any experience with using this, so I’m left with the man  
page.

## Eric

Are you interested in on-site Ruby training that’s been highly  
reviewed by former students? [http://LearnRuby.com](http://LearnRuby.com)

---

<div class="post-metadata">

**Author:** ![Hans\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@Hans\_F](https://www.ruby-forum.com/u/Hans_F)\
**Post date:** [February 22, 2008, 2:00am UTC](https://www.ruby-forum.com/t/timers-and-alarms/129789/4 "2008-02-22T02:00:56Z")

</div>

Eric I. wrote:

> > > end
> 
> Which OS are you using? On OS X the man page for setitimer claims  
> there are three separate timers, one of which uses real time. From  
> the man page:
> 
> ```
> The ITIMER_REAL timer decrements in real time. A SIGALRM signal
> 
> ```
> 
> is  
> delivered when this timer expires.
> 
> I don’t have any experience with using this, so I’m left with the man  
> page.

OS X, and experimentation verifies the same behavior as sleep. It’s  
probably the same in Linux, but I don’t have a linux laptop to try it  
with.

---

<div class="post-metadata">

**Author:** ![Hans\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@Hans\_F](https://www.ruby-forum.com/u/Hans_F)\
**Post date:** [February 21, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/timers-and-alarms/129789/5 "2008-02-21T19:01:03Z")

</div>

On Feb 21, 8:42 am, “Eric I.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> remaining = end\_time - Time.now  
> while granularity \< remaining  
> sleep granularity  
> remaining = end\_time - Time.now  
> end
> 
> sleep remaining  
> end

It’s going to have to be something like this I think. I don’t know how  
much impact waking every so often (about once per second in my  
situation) will have on power usage, but I see now that even setitimer  
in C suffers the same problem sleep does with laptop suspend.
