Doing something each second

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

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

2009/6/9 Jamey C. [email protected]:

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

On Tue, Jun 9, 2009 at 7:59 AM, Christoph
Jasinski[email protected] 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

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

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

raise ArgumentError unless @reality.responds_to? :reason

On Tue, Jun 9, 2009 at 3:08 PM, Christoph J. <
[email protected]> 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