有没有什么办法可以让消费者临时暂停,稍后再恢复?
下面是我想要做的一个例子:
require "bunny"
conn = Bunny.new
conn.start
ch1 = conn.create_channel
publisher = ch.direct('test', :auto_delete => false)
consumer1 = nil
Thread.new do
ch2 = conn.create_channel(nil, 8) #Using eight worker
queue1 = ch2.queue('', :exclusive => true)
queue1.bind(publisher, :routing_key => 'low_priority')
consumer1 = queue1.subscribe(:block => true) do |delivery_info, properties, payload|
#do some work
end
end
Thread.new do
ch3 = conn.create_channel
queue2 = ch3.queue('', :exclusive => true)
queue2.bind(publisher, :routing_key => 'high_priority')
consumer2 = queue2.subscribe(:block => true) do |delivery_info, properties, payload|
consumer1.pause #pause the other consumer
#do other things
consumer1.resume #resume the consumer
end
end
#rest of the code当我在consumer2工作时,我想暂停consumer1。有什么有效的方法可以做到这一点吗?
发布于 2016-04-25 05:33:18
如果你想创建优先级策略,bunny已经实现了:
http://rubybunny.info/articles/queues.html#consumer_priorities
q = ch.queue("a.queue")
q.subscribe(:manual_ack => true, :arguments => {"x-priority" => 5}) do |delivery_info, properties, payload|
# ...
end
q.subscribe(:manual_ack => true, :arguments => {"x-priority" => 2}) do |delivery_info, properties, payload|
# ...
end如果你想并行处理它,我推荐这个gem:
https://github.com/grosser/parallel
示例:
results = Parallel.map(['a','b','c'], in_processes: 3) { |one_letter| ... }希望能有所帮助。
https://stackoverflow.com/questions/35791258
复制相似问题