簡體   English   中英

如何在兔子中暫停和恢復消費者

[英]How to pause and resume consumer in bunny

有什么方法可以暫時暫停使用者並在以后恢復它?

這是我想做的一個例子:

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。 有什么有效的方法嗎?

如果要創建優先級策略,則兔子已實現了以下功能:

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| ... }

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM