doing something each second

  • Thread starter Christoph Jasinski
  • Start date
C

Christoph Jasinski

[Note: parts of this message were removed to make it a legal post.]

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
 
J

Jamey Cribbs

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

while true
sleep 1
stuff
end
 
R

Robert Klemme

2009/6/9 Jamey Cribbs said:
Off the top of my head, you can do something like:

while true
=A0sleep 1
=A0stuff
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


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
C

Charles Oliver Nutter

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 {
=C2=A0stuff
}

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
 
E

Eleanor McHugh

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
 
C

Christoph Jasinski

[Note: parts of this message were removed to make it a legal post.]

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
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

On Tue, Jun 9, 2009 at 3:08 PM, Christoph Jasinski <
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top