Is there any method to temporarily pause a consumer and resume it at later time?
Here is an example of what i want to do:
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
I want to pause consumer1 when I'm doing the work at consumer2. Is there any valid way to do that?
If you want to create priority policies, bunny has implemented this:
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
And if you want to process it in parallel I suggest this gem:
https://github.com/grosser/parallel
Example:
results = Parallel.map(['a','b','c'], in_processes: 3) { |one_letter| ... }
Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.