title: Listening for RabbitMQ and running flask on same server
body: <p>I have a Flask application as such</p>
  
  <pre class="lang-py prettyprint-override"><code>from flask import Flask
  from flask_restful import Resource, Api
  from mq_handler import MessageBroker
  import pika
  
  app = Flask(__name__)
  api = Api(app)
  connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
  mb = MessageBroker(connection)
  
  
  
  class HelloWorld(Resource):
      def get(self):
          mb.run()
          return {'hello': 'world'}
  
  
  class LogHandler(Resource):
      def get(self, table):
          return {'TableName': table}
  
  
  
  
  api.add_resource(HelloWorld, '/')
  api.add_resource(LogHandler, '/log/&lt;string:table&gt;')
  
  
  
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>
  
  <p>I have added a MessageBroker class to handle all my rabbitMq messages</p>
  
  <pre class="lang-py prettyprint-override"><code>import pika
  import json
  
  
  class MessageBroker:
      def __init__(self, connection):
          self.connection = connection
          self.channel = connection.channel()
  
      def run(self):
          self.channel.start_consuming()
          self.channel.basic_consume(queue='logs',
                                auto_ack=True,
                                on_message_callback=self.handle_log)
          self.channel.start_consuming()
  
      def handle_log(self, ch, method, properties, body):
          decoded_content = body.decode('utf-8')
          json_payload = json.loads(decoded_content)
          print(" [x] Received %r" % json_payload['message'])
  
  </code></pre>
  
  <p>I have tried different solutions, but have can I get both services to run simultaneously on the same server? can somebody explain that please?</p>
  
  <p>In general.. how is it possible to have several services running listening on my flask server?</p>