使用Daemons gem运行代码块

时间:2013-04-01 19:01:02

标签: ruby process daemon

有人可以解释为什么以下代码不会产生传递的块吗?

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'

Ruby 1.9.3p392,x86_64 Linux

1 个答案:

答案 0 :(得分: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'