# Doing something each second

**URL:** <https://www.ruby-forum.com/t/doing-something-each-second/169844>\
**Category:** Ruby\
**Created:** [June 9, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/doing-something-each-second/169844 "2009-06-09T14:59:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/doing-something-each-second/169844/1 "2009-06-09T14:59:47Z")

</div>

Hi,  
is it possible to execute a block each second for let’s say 8 hours a  
day?  
Instead of using threads maybe. Something like this:

Time.each.second do {  
stuff  
}

Thanks

Cheers,

Chris

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 3:42pm UTC](https://www.ruby-forum.com/t/doing-something-each-second/169844/2 "2009-06-09T15:42:38Z")

</div>

Off the top of my head, you can do something like:

while true  
sleep 1  
stuff  
end

On Tue, Jun 9, 2009 at 8:59 AM, Christoph

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 4:22pm UTC](https://www.ruby-forum.com/t/doing-something-each-second/169844/3 "2009-06-09T16:22:00Z")

</div>

2009/6/9 Jamey C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Off the top of my head, you can do something like:
> 
> while true  
> sleep 1  
> stuff  
> end

Note thought hat if stuff takes longer you will execute something with  
1 second _pauses_ which is not the same as execute something each  
second.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/doing-something-each-second/169844/4 "2009-06-09T18:13:48Z")

</div>

On Tue, Jun 9, 2009 at 7:59 AM, Christoph  
Jasinski[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> is it possible to execute a block each second for let’s say 8 hours a day?  
> Instead of using threads maybe. Something like this:
> 
> Time.each.second do {  
> Â stuff  
> }

There’s no way to run something asynchronously every second or trigger  
something to run asynchronously every second without a thread of some  
kind or without interrupting the flow of the main thread. Why don’t  
you want to use a thread?

- Charlie

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/doing-something-each-second/169844/5 "2009-06-09T23:09:25Z")

</div>

Thanks to all of you. I actually started out using a thread but then got  
into some trouble, which some people helped me to solve. I was just  
thinking  
that there might be a way of using Time.second.each do {…} because I  
saw  
5.times do {…}. That was just a “creative” conclusion.  
Chris

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/doing-something-each-second/169844/6 "2009-06-09T19:24:49Z")

</div>

On 9 Jun 2009, at 17:13, Charles Oliver N. wrote:

> There’s no way to run something asynchronously every second or trigger  
> something to run asynchronously every second without a thread of some  
> kind or without interrupting the flow of the main thread. Why don’t  
> you want to use a thread?

It could probably be done with a signal handler, but that’s not the  
kind of code anyone wants to maintain in its raw state. However this  
does look like a nice idea for a thread abstraction…

Ellie

## Eleanor McHugh Games With Brains [http://slides.games-with-brains.net](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 10, 2009, 1:38am UTC](https://www.ruby-forum.com/t/doing-something-each-second/169844/7 "2009-06-10T01:38:46Z")

</div>

On Tue, Jun 9, 2009 at 3:08 PM, Christoph J. \<  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

> Thanks to all of you. I actually started out using a thread but then got  
> into some trouble, which some people helped me to solve. I was just  
> thinking  
> that there might be a way of using Time.second.each do {…} because I saw  
> 5.times do {…}. That was just a “creative” conclusion.  
> Chris

You could do something like this with an asynchronous event framework  
like  
EventMachine or Rev without using a thread, although if what you’re  
trying  
to do every second takes longer than a second you will likely get…  
strange  
behavior.

Here’s an example with Rev:

class MyTimer \< Rev::TimerWatcher  
def on\_timer  
do\_something  
end  
end

watcher = MyTimer.new(1, true) # 1 second repeating  
watcher.attach Rev::Loop.default

Rev::Loop.default.run
