<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1138.47">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #f9f9f9}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #726d73}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #ac8257}
p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #f9f9f9; min-height: 15.0px}
p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #a0ab82}
p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #d7b583}
span.s1 {color: #d7b583}
span.s2 {color: #a0ab82}
span.s3 {color: #f9f9f9}
span.s4 {color: #ac8257}
span.s5 {color: #da7e64}
span.s6 {text-decoration: underline}
span.s7 {color: #726d73}
</style>
<p class="p1">I am writing a consumer (and producer) using Pika. I want to write asynchronous code, but I'm having trouble getting everything working. I started with regular I pulled off the Pika docs, then made it work with multiple queues. To get it working with multiple queues I had to add some checks that make sure all the queues are declared and bound before trying to publish any messages. That worked very well.</p>
<p class="p2"><br></p>
<p class="p1">Our code also performs some time consuming logic, and we want our consumer to publish to another queue that will go to some logger. I tried to add this on the same channel that was consuming code, but that did not work. I wasn't sure if I needed a separate channel, so I tried using 2 channels on the same connections - 1 channel to consume, and 1 channel to produce, but that is not working. I notice when the code is trying to publish the 2nd log message that everything seems to stop. Can anyone give me any insight into what's going on?</p>
<p class="p2"><br></p>
<p class="p1">Attached are sections of my code:</p>
<p class="p2"><br></p>
<p class="p1">This code is setup so a lot of the multiple queue setup is handled without duplicating any code:</p>
<p class="p3"><font class="Apple-style-span" color="#000000"><span class="s1">from</span> pika <span class="s1">import</span> spec</font></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p3"><font class="Apple-style-span" color="#000000">EXCHANGE = <span class="s2">'customers'</span></font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3">EXCHANGEACK = </span><span class="s2">'logs'</span><span class="s3"> </span>#keep this seperate for now</font></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p6"><font class="Apple-style-span" color="#000000"><span class="s1">class</span><span class="s3"> </span>ChannelContainer<span class="s3">(object):</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">def</span> <span class="s4">__init__</span>(<span class="s1">self</span>, channel, exchange, append, queue_list, callback):</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel = channel</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.exchange = exchange</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.append_str = append</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.queue_list = queue_list</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.goodQueues = <span class="s5">0</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channelReady = <span class="s1">False</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.try_to_continue = callback</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p6"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">def</span><span class="s3"> </span>exchange_declare<span class="s3">(</span><span class="s1">self</span><span class="s3">):</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel.exchange_declare(exchange=<span class="s1">self</span>.exchange,</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> type=<span class="s2">'direct'</span>, \</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> callback=<span class="s1">self</span>.on_exchange_declare)</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Step #4</font></p>
<p class="p6"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">def</span><span class="s3"> </span>on_exchange_declare<span class="s3">(</span><span class="s1">self</span><span class="s3">, frame):</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Called when our exchange has been created</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """<span class="s3"> </span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">for</span> queue_name <span class="s1">in</span> <span class="s1">self</span>.queue_list:</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> full_queue_name = queue_name + <span class="s1">self</span>.append_str</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel.queue_declare(queue=full_queue_name, durable=<span class="s1">True</span>, \</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> exclusive=<span class="s1">False</span>, auto_delete=<span class="s1">False</span>, \</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> callback=<span class="s1">self</span>.on_queue_declared)</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Step #5</font></p>
<p class="p6"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">def</span><span class="s3"> </span>on_queue_declared<span class="s3">(</span><span class="s1">self</span><span class="s3">, frame):</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Bind the queues to the channel</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">if</span> type(frame.method) == spec.Queue.DeclareOk:</font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Get the queue name</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> queue_name = frame.method.queue</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">print</span> <span class="s1">self</span>.append_str, <span class="s2">"binding"</span>, queue_name</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel.queue_bind(exchange=<span class="s1">self</span>.exchange,</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> queue=queue_name,</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> routing_key=queue_name, </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> callback=<span class="s1">self</span>.on_queue_bound)</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Step #6</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">def</span> <span class="s4">on_queue_bound</span>(<span class="s1">self</span>, frame):</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Called when RabbitMQ has told us our Queue has been declared,</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> frame is the response from RabbitMQ</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">print</span><span class="s3"> </span>"bound queue"</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.goodQueues += <span class="s5">1</span></font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">if</span> <span class="s1">self</span>.goodQueues == len(<span class="s1">self</span>.queue_list):</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channelReady = <span class="s1">True</span></font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span>#tell owner to try to continue</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.try_to_continue()</font></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p3"><font class="Apple-style-span" color="#000000"><span class="s1">class</span> <span class="s4">TxChannel</span>(ChannelContainer):</font></p>
<p class="p4"><br></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">def</span> <span class="s4">__init__</span>(<span class="s1">self</span>, channel, exchange, append, queue_list, callback):</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> ChannelContainer.__init__(<span class="s1">self</span>, channel, exchange, append, queue_list, callback)</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">def</span> <span class="s4">acknowledge</span>(<span class="s1">self</span>, queue_name, body):</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Send the action and object to the acknowledgement queue.</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> full_queue_name = <span class="s2">'%s--ack'</span> % queue_name</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">print</span> <span class="s2">"sending ack"</span>, full_queue_name</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel.basic_publish(exchange=<span class="s1">self</span>.exchange,</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> routing_key=full_queue_name,</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> body=body)</font></p>
<p class="p4"><br></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p4"><br></p>
<p class="p3"><font class="Apple-style-span" color="#000000"><span class="s1">class</span> <span class="s4">RxChannel</span>(ChannelContainer):</font></p>
<p class="p4"><br></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">def</span> <span class="s4">__init__</span>(<span class="s1">self</span>, channel, exchange, append, queue_list, callback):</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> ChannelContainer.__init__(<span class="s1">self</span>, channel, exchange, append, queue_list, callback)</font></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Step #6</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">def</span> <span class="s4">consume</span>(<span class="s1">self</span>, callback):</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Called when RabbitMQ has told us our Queue has been declared,</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> frame is the response from RabbitMQ</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p4"><br></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span>#consume on all queues now</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">for</span> queue_name <span class="s1">in</span> <span class="s1">self</span>.queue_list:</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel.basic_consume(callback, queue=queue_name)</font></p>
<p class="p7"> </p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p9">Here is the consumer code. I tried to take out everything irrelevant:</p>
<p class="p10"><font class="Apple-style-span" color="#000000">import<span class="s3"> pika</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"><span class="s1">from</span> pika.adapters <span class="s1">import</span> SelectConnection</font></p>
<p class="p4"><br></p>
<p class="p3"><font class="Apple-style-span" color="#000000"><span class="s1">from</span> ChannelContainer <span class="s1">import</span> <span class="s6">RxChannel</span>, <span class="s6">TxChannel</span></font></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p3"><font class="Apple-style-span" color="#000000">EXCHANGE = <span class="s2">'customers'</span></font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3">EXCHANGEACK = </span><span class="s2">'logs'</span><span class="s3"> </span>#keep this seperate for now</font></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p6"><font class="Apple-style-span" color="#000000"><span class="s1">class</span><span class="s3"> </span>RabbitConsumer<span class="s3">(object):</span></font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p6"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">def</span><span class="s3"> </span>__init__<span class="s3">(</span><span class="s1">self</span><span class="s3">):</span></font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span>#initialize some variables</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.queue_list = []</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.goodQueues = <span class="s5">0</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel_rx = <span class="s1">None</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel_tx = <span class="s1">None</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.connection = <span class="s1">None</span></font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.parse_config()</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span>#get the queue_list</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.gather_queue_names()</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Functions to parse data and load config</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p4"><br></p>
<p class="p6"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">def</span><span class="s3"> </span>parse_config<span class="s3">(</span><span class="s1">self</span><span class="s3">):</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Parse the variables in the config file into the instance.</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.rabbitmq_host = <span class="s2">'localhost'</span></font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p6"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">def</span><span class="s3"> </span>gather_queue_names<span class="s3">(</span><span class="s1">self</span><span class="s3">):</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Gather the names of the queues.</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span>#These are just for testing</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.queue_list = [<span class="s2">'1a'</span>, <span class="s2">'2b'</span>, <span class="s2">'3c-4d-5e-6f-7g'</span>]</font></p>
<p class="p4"><br></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Functions to connect to RabbitMQ</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Step #2</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">def</span> <span class="s4">on_connected</span>(<span class="s1">self</span>, connection):</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Called when we are fully connected to RabbitMQ</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Open a channel for tx and rx</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.connection.channel(<span class="s1">self</span>.on_channel_open)</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.connection.channel(<span class="s1">self</span>.on_channel_open)</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Step #3</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">def</span> <span class="s4">on_channel_open</span>(<span class="s1">self</span>, new_channel):</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Called when our channel has opened</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">if</span> <span class="s1">self</span>.channel_rx <span class="s1">is</span> <span class="s1">None</span>:</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s7">#rx channel</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel_rx = RxChannel(new_channel, EXCHANGE, </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">''</span>, <span class="s1">self</span>.queue_list, <span class="s1">self</span>.start_listening)</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel_rx.exchange_declare()</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">else</span>:</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel_tx = TxChannel(new_channel, EXCHANGEACK, </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">'--ack'</span>, <span class="s1">self</span>.queue_list, <span class="s1">self</span>.start_listening)</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel_tx.exchange_declare()</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Step #7</font></p>
<p class="p6"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">def</span><span class="s3"> </span>start_listening<span class="s3">(</span><span class="s1">self</span><span class="s3">):</span></font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s2">"""</span></font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> Called when all queues for a channel are declared</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"> """</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">print</span><span class="s3"> </span>"test_allset"</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">if</span> <span class="s1">self</span>.channel_rx.channelReady <span class="s1">and</span> <span class="s1">self</span>.channel_tx.channelReady:</font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span>#both channels are ready, try to continue</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel_rx.consume(<span class="s1">self</span>.handle_consume)</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">def</span> <span class="s4">handle_consume</span>(<span class="s1">self</span>, channel, method, properties, body):</font></p>
<p class="p8"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">print</span><span class="s3"> </span>"received message"</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> queue_name = method.routing_key</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Insert some long and complicated code goes here</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> result = <span class="s2">'I finished!'</span></font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span>#have the tx channel send an ack</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.channel_tx.acknowledge(queue_name, result)</font></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Step #1</font></p>
<p class="p6"><font class="Apple-style-span" color="#000000"><span class="s3"> </span><span class="s1">def</span><span class="s3"> </span>start_consumer<span class="s3">(</span><span class="s1">self</span><span class="s3">):</span></font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Step #1: Connect to RabbitMQ</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.connection = SelectConnection(pika.ConnectionParameters(\</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> host=<span class="s1">self</span>.rabbitmq_host), <span class="s1">self</span>.on_connected)</font></p>
<p class="p7"><font class="Apple-style-span" color="#000000"> </font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">try</span>:</font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Loop so we can communicate with RabbitMQ</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.connection.ioloop.start()</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">except</span> KeyboardInterrupt:</font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Gracefully close the connection</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.connection.close()</font></p>
<p class="p5"><font class="Apple-style-span" color="#000000"><span class="s3"> </span># Loop until we're fully closed, will stop on its own</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> <span class="s1">self</span>.connection.ioloop.start()</font></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p3"><font class="Apple-style-span" color="#000000"><span class="s1">if</span> __name__ == <span class="s2">'__main__'</span>:</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> consumer = RabbitConsumer()</font></p>
<p class="p3"><font class="Apple-style-span" color="#000000"> consumer.start_consumer()</font></p><div><br></div><div>Thanks!</div><div><br></div>-- <br><span style="font-family:Helvetica,Arial,sans-serif;font-size:14px;line-height:16px">Charles Law</span><div>
Watch how we make online advertising simple: <a href="http://bit.ly/Ent_vid" style="color:rgb(0,114,188);text-decoration:none" target="_blank">http://bit.ly/Ent_vid</a></div><div><a href="http://www.openx.org/" style="color:rgb(0,114,188);text-decoration:none" target="_blank">www.openx.com</a> | follow us on: <a href="http://twitter.com/openx" style="color:rgb(0,114,188);text-decoration:none" target="_blank">Twitter</a> <a href="http://www.facebook.com/OpenX" style="color:rgb(0,114,188);text-decoration:none" target="_blank">Facebook</a> <a href="http://www.linkedin.com/company/openx/products" style="color:rgb(0,114,188);text-decoration:none" target="_blank">LinkedIn</a>
</div><br>