gayavat
gayavat

Reputation: 19408

em.synchrony in thin server returns result after it needs to be used

Thin server is based on EventMachine, therefore I can't stop it from EM.synchrony block. But in this case output is: 2, 1 but not 1,2. How can I fix it?

 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"

Thanks for advance!

Upvotes: 4

Views: 465

Answers (1)

phil pirozhkov
phil pirozhkov

Reputation: 4900

EM does not guarantee the order of execution due to its asynchronous nature. 1 is only executed after FiberIterator has done its job, while 2 is executed earlier. if you need 2 to be executed after 1, put it inside EM.synchrony block.

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"

Upvotes: 2

Related Questions