title: Listen RabbitMQ queue in Flask view
body: <p>I have a view in which i send one message (request id) to workers. Then i need to listen result queue and return response only when i received a response msg (The response must contain this msg). How do i do it correctly?</p>
  
  <p>This is my code:</p>
  
  <pre><code>def get(self):
      ruid = rand_ruid()
      # add msg to q1
      write_ch = current_app.amqp_conn.channel()
      write_ch.queue_declare(queue='q1', durable=True)
      msg = mkmsg(ruid=ruid)
      write_ch.basic_publish(exchange='', routing_key='q1', body=msg)
      write_ch.close()
  
      # then wait results from qbus
      listen_ch = current_app.amqp_conn.channel()
      listen_ch.exchange_declare(exchange='exbus', type='direct')
      listen_ch.queue_declare(queue='bus')
  
      listen_ch.queue_bind(exchange='exbus', queue='bus', routing_key=ruid)
      while 1:
          for method, properties, body in listen_ch.consume('bus'):
              if method and body:
                  listen_ch.basis_ack(delivery_tag=method.delivery_tag)
                  listen_ch.close()
                  return make_response(body)
  </code></pre>
  
  <p><strong>Updated</strong>
  My question is incorrect, as my approach. I wanted to do asynchronous action (waiting for result from the queue) in synchronous part of the program (flask view).</p>