2013-04-01 44 views
0

有人可以解釋爲什麼下面的代碼不會產生傳入的塊?運行代碼塊與守護進程gem

require 'daemons' 

t = Daemons.call do 
    # This block does not start 
    File.open('out.log','w') do # code don't get here to open a file 
    |fw| 
    10.times { 
     fw.puts "=>#{rand(100)}" 
     sleep 1 
    } 
    end 
end 
#t.start # has no effect 
10.times { 
    puts "Running ? #{t.running?}" # prints "Running ? false" 10 times 
    sleep 1 
} 
t.stop 
puts 'finished' 

紅寶石1.9.3p392,x86_64的Linux的

回答

0

你確定你不是想運行併發編程Thread

這裏是一個Thread實施會是什麼樣子:

f = File.open('out.log', 'w') 
t = Thread.new do 
    10.times { 
    f.puts "=>#{rand(100)}" 
    sleep 1 
    } 
end 
10.times { 
    puts "Running ? #{t.alive?}" 
    sleep 1 
} 
t.exit 
puts 'finished'