Poncho Quintanilla wrote:
> I'm trying to schedule some task using ruby threads
> 
> This work great:
> 
> t = Thread.new do
>   while true do
>     puts "Starting new job at #{Time.now}"
>     do_something
>     sleep 3
>   end
> end
> t.join
> 
> But when the task take a long, like:
> 
> def do_something
>   puts "doing something at#{Time.now}"
>   sleep(rand(3))
> end
> 
> I cant get the "Starting new job" message appears in regular intervals
> of 3 seconds. What I'm missing?

It works just fine for me (ruby-1.8.6p114, ubuntu hardy). Note that 
rand(3) returns either 0, 1 or 2; it's not going to be easy to see the 
delay varying.

It's clearer if you print Time.now.to_f

Starting new job at 1228381283.09741
doing something at1228381283.12344
Starting new job at 1228381288.13136   << + 5
doing something at1228381288.13152
Starting new job at 1228381291.13536   << + 3
doing something at1228381291.13553
Starting new job at 1228381296.14337   << + 5
doing something at1228381296.14353

Or change rand(3) to rand(10).
-- 
Posted via http://www.ruby-forum.com/.