How to implement an endless loop in eventmachine

I need to have an infinite loop on top of an eventmachine that constantly reads the redis queue. below is my code. Is recursion the right way to do this? I tried the loop do , but couldn't get it to work this way.

 require 'em-hiredis' def read d = @redis.blpop 'queue', 0 d.callback do |_, value| p value read end.errback do |e| pe EM.next_tick { read } end end EM.run do @redis = EM::Hiredis.connect read end 
+4
source share
1 answer

Better sign up for redis pub / sub queue. https://gist.github.com/957367 If you really need a loop, then EM itself is an endless loop, you just need to plan your work again and again with next_tick:

 def read d = @redis.blpop 'queue', 0 d.callback do |_, value| EM.next_tick { read } end.errback do |e| EM.next_tick { read } end end 
+3
source

All Articles