title: RabbitMQ, Pika, and Twisted Reactor
body: <p>I am having a problem with stopping my reactor while running a publisher to publish one message.  It seems like the reactor.stop() function executes before the message is actually sent and makes it on the queue.  Logically I think this makes sense, but it stops the code before the message makes it onto the queue.  When I take out the reactor.stop() the message makes it to the queue, but the reactor can't be stopped at all at this point. </p>
  
  <pre><code>@inlineCallbacks
  def do_msg(connection):
      try:
          msg = content
          print "Connected to broker."
  
          chan = yield connection.channel()
          print "Channel constructed."
  
          # Declare exchange
          yield chan.exchange_declare(exchange="test_exchange", 
                    exchange_type="topic", durable=True)
          print "Exchange opened"
  
          # Declare queue
          yield chan.queue_declare(queue="test_queue", durable=True, 
                     exclusive=False)
          print "Queue opened."
  
          # Bind exchange and queue
          yield channel.queue_bind(exchange="test_exchange", 
                     queue="test_queue", routing_key="test_key")
          print "Exchange and Queue bound"
  
          print "Sending message: %s" % msg
          yield chan.basic_publish(exchange="test_exchange", body=msg, 
                   routing_key="test_key",                               
                   properties=BasicProperties(delivery_mode = 2))
  
      except Exception, e:
          print "ERROR: %s" % str(e)
      finally:
          yield connection.close()
          reactor.stop() # problem occurs here
  </code></pre>
  
  <p>Does anyone know a way to fix this? Is there a way to stop the reactor based on a condition that the message got to the broker?</p>