[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

doing something each second

Christoph Jasinski

6/9/2009 12:59:00 PM

[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

6 Answers

Jamey Cribbs

6/9/2009 1:42:00 PM

0

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
Jasinski<christoph.jasinski@googlemail.com> 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 {
> =A0stuff
> }
>
> Thanks
>
> Cheers,
>
> Chris
>

Robert Klemme

6/9/2009 2:21:00 PM

0

2009/6/9 Jamey Cribbs <jcribbs@netpromi.com>:
> 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.rubybestprac...

Charles O Nutter

6/9/2009 4:13:00 PM

0

On Tue, Jun 9, 2009 at 7:59 AM, Christoph
Jasinski<christoph.jasinski@googlemail.com> 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 {
> =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

Eleanor McHugh

6/9/2009 5:24:00 PM

0

On 9 Jun 2009, at 17:13, Charles Oliver Nutter wrote:
> On Tue, Jun 9, 2009 at 7:59 AM, Christoph
> Jasinski<christoph.jasinski@googlemail.com> 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?

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-...
----
raise ArgumentError unless @reality.responds_to? :reason


Christoph Jasinski

6/9/2009 9:09:00 PM

0

[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

Tony Arcieri

6/9/2009 11:38:00 PM

0

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

On Tue, Jun 9, 2009 at 3:08 PM, Christoph Jasinski <
christoph.jasinski@googlemail.com> 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

--
Tony Arcieri
medioh.com