Continous running thread

Hi,

I have a simple task, I want to have a Thread that will every 2 minutes
perfom a task. I have tried:

  thread = Thread.new do
    while true
       puts "Scott"
       sleep 10000
    end
end

But it seems to only print "scott" once. How can I in Ruby have a
thread that will puts "scott" ervery 10 seconds?

Thanks in advance.

  thread = Thread.new do
    while true
       puts "Scott"
       sleep 10000
    end
end

Change 10000 to 10.0 ?

Caleb

Hi,

I have a simple task, I want to have a Thread that will every 2 minutes
perfom a task. I have tried:

  thread = Thread.new do
    while true
       puts "Scott"
       sleep 10000
    end
end

But it seems to only print "scott" once. How can I in Ruby have a
thread that will puts "scott" ervery 10 seconds?

Hi

sleep sleeps for seconds not for milliseconds. Try slee 10

Cheers
detlef

···

On Fr, 2005-11-04 at 03:02 +0900, iamscottwalter@gmail.com wrote:

Thanks in advance.

iamscottwalter@gmail.com wrote:

Hi,

I have a simple task, I want to have a Thread that will every 2 minutes
perfom a task. I have tried:

  thread = Thread.new do
    while true
       puts "Scott"
       sleep 10000
    end
end

But it seems to only print "scott" once. How can I in Ruby have a
thread that will puts "scott" ervery 10 seconds?

You have to join the thread, or otherwise ensure the process keeps running. Also 10000 is 10 thousand seconds, or 2-3/4 hours.

s/10000/10/

-mental

···

On Fri, 2005-11-04 at 03:02 +0900, iamscottwalter@gmail.com wrote:

Hi,

I have a simple task, I want to have a Thread that will every 2 minutes
perfom a task. I have tried:

  thread = Thread.new do
    while true
       puts "Scott"
       sleep 10000
    end
end

But it seems to only print "scott" once. How can I in Ruby have a
thread that will puts "scott" ervery 10 seconds?

Ok I changed it to:

thread = Thread.new do
     while true
        puts "Scott"
        sleep 10.0
     end
end

However, it only executed once and exits. It doesn't even wait 10
seconds before exiting. This is driving me crazy.

Bob Showalter wrote:

···

iamscottwalter@gmail.com wrote:

Hi,

I have a simple task, I want to have a Thread that will every 2
minutes perfom a task. I have tried:

  thread = Thread.new do
    while true
       puts "Scott"
       sleep 10000
    end
end

But it seems to only print "scott" once. How can I in Ruby have a
thread that will puts "scott" ervery 10 seconds?

You have to join the thread, or otherwise ensure the process keeps
running. Also 10000 is 10 thousand seconds, or 2-3/4 hours.

Yes joining is the main problem here. But if Bob makes his main thread
join this thread he doesn't need another thread at all. :slight_smile:

Kind regards

    robert

sleep only takes integer values, but I don't think that's your problem

try this though.

thread = Thread.new do
   loop do
      puts "Scott"
      sleep 10
   end
end
thread.join

···

On 11/3/05, iamscottwalter@gmail.com <iamscottwalter@gmail.com> wrote:

However, it only executed once and exits. It doesn't even wait 10
seconds before exiting. This is driving me crazy.

iamscottwalter@gmail.com wrote:

Ok I changed it to:

thread = Thread.new do
     while true
        puts "Scott"
        sleep 10.0
     end
end

However, it only executed once and exits. It doesn't even wait 10
seconds before exiting. This is driving me crazy.

As others have mentioned:

thread = Thread.new do
      while true
         puts "Scott"
         sleep 10.0
      end
  end

# Add this:
thread.join

James

···

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys

iamscottwalter@gmail.com wrote:

Ok I changed it to:

thread = Thread.new do
    while true
       puts "Scott"
       sleep 10.0
    end
end

However, it only executed once and exits. It doesn't even wait 10
seconds before exiting. This is driving me crazy.

Hi Scott,

If you look at one of the previous responses (from Bob Showalter) youl'l find your answer. Try adding:
thread.join
to the end of your script.

You may also need to flush the IO at some point to get the timing of the output to work correctly.

Hope this is helpful.

Matthew

sleep only takes integer values, but I don't think that's your problem

Nah, it's perfectly happy taking a Float too.

Caleb

That did the trick! Thanks.

Sleep will round you to the nearest integer.

Run sleep 1.4 and sleep 1.9 in irb and see what you get.

···

On 11/3/05, Caleb Tennis <caleb@aei-tech.com> wrote:

>
> sleep only takes integer values, but I don't think that's your problem

Nah, it's perfectly happy taking a Float too.

Sleep will round you to the nearest integer.

Run sleep 1.4 and sleep 1.9 in irb and see what you get.

It rounds its return value to the nearest integer, but at least here it sleeps
for the specified time.

try sleep 0.0, sleep 0.2, and sleep 1.0 and see the difference.

Caleb

Sleep will round you to the nearest integer.

That's just the return value.

Run sleep 1.4 and sleep 1.9 in irb and see what you get.

You can sleep for fractional seconds. Try 0.1 and 0.9. You should be able to see the difference.

James Edward Gray II

···

On Nov 3, 2005, at 12:39 PM, Gregory Brown wrote:

> Sleep will round you to the nearest integer.
>
> Run sleep 1.4 and sleep 1.9 in irb and see what you get.

It rounds its return value to the nearest integer, but at least here it sleeps
for the specified time.

I just looked it up in the Pickaxe... you're right. It returns the
time actually slept (rounded to the nearest second) and tries to sleep
to the specified float time.

http://www.rubycentral.com/book/ref_m_kernel.html#Kernel.sleep

This is not meant to a precise I don't think, so this kind of round is fine.

I once needed greater precision than sleep() but I forgot what I used.
I am pretty sure there is something in the standard library for this,
though.

···

On 11/3/05, Caleb Tennis <caleb@aei-tech.com> wrote: