瘦服务器基于EventMachine,因此我无法从EM.synchrony块中阻止它.
但在这种情况下,输出为:2,1,但不是1,2.我该如何解决?
EM.synchrony do
update_ads ads_hash, invalid_ads, ip, params, user_agent, request
EM::Synchrony::FiberIterator.new([1,2,3]).each do |i|
# some http requests
end
puts "1"
# EventMachine.stop
end
puts "2"
谢谢你提前!
最佳答案 EM由于其异步性质而无法保证执行顺序. 1仅在FiberIterator完成其工作后执行,而2则先执行.如果您需要在1之后执行2,请将其放在EM.synchrony块中.
UPD:
x = EM.synchrony do
# update_ads ads_hash, invalid_ads, ip, params, user_agent, request
EM::Synchrony::FiberIterator.new([1,2,3]).each do |i|
# some http requests
end
"1"
end
puts x
puts "2"