我想在EventMachine中使用ActiveRecord对大型数据集执行批量数据库查询。我希望传递给find_each的块的每次调用都在EventMachine周期定时器中调用。
使用以下命令,find_each只运行,add_periodic_timer块只运行一次,直到find_each完全结束(即周期性定时器块不会每0.001秒运行一次):
EventMachine.add_periodic_timer(0.001) do
TradingCore::Quote.by_date(@date).by_symbols(@symbols).order(:created_at).find_each do |quote|
...
sleep(delay)
end
end
有没有办法在不阻塞事件循环的情况下为每条记录执行find_each块?
答案 0 :(得分:0)
想想我用光纤解决了这个问题。所以,我做了这个
quotes_fiber = Fiber.new {
TradingCore::Quote.by_date(@date).by_symbols(@symbols).order(:created_at).find_each do |quote|
...
sleep(delay)
Fiber.yield
end
}
EventMachine.add_periodic_timer(0.001) do
quotes_fiber.resume
end
事情似乎工作正常:)