Forum: Ruby doing something each second

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Christoph J. (Guest)
on 2009-06-09 16:59
(Received via mailing list)
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
Jamey C. (Guest)
on 2009-06-09 17:42
(Received via mailing list)
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
Robert K. (Guest)
on 2009-06-09 18:22
(Received via mailing list)
2009/6/9 Jamey C. <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
Charles N. (Guest)
on 2009-06-09 20:13
(Received via mailing list)
On Tue, Jun 9, 2009 at 7:59 AM, Christoph
Jasinski<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
Eleanor McHugh (Guest)
on 2009-06-09 21:24
(Received via mailing list)
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
Christoph J. (Guest)
on 2009-06-10 01:09
(Received via mailing list)
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
Tony A. (Guest)
on 2009-06-10 03:38
(Received via mailing list)
On Tue, Jun 9, 2009 at 3:08 PM, Christoph J. <
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
This topic is locked and can not be replied to.