Using Tornado with Pika for Asynchronous Queue Monitoring

前端 未结 2 1336
半阙折子戏
半阙折子戏 2021-02-06 06:51

I have an AMQP server (RabbitMQ) that I would like to both publish and read from in a Tornado web server. To do this, I figured I would use an asynchronous amqp python library;

相关标签:
2条回答
  • 2021-02-06 07:23

    It would help to see some source code, but I use this same tornado-supporting pika module without issue in more than one production project.

    You don't want to create a connection per request. Create a class that wraps all of your AMQP operations, and instantiate it as a singleton at the tornado Application level that can be used across requests (and across request handlers). I do this in a 'runapp()' function that does some stuff like this and then starts the main tornado ioloop.

    Here's a class called 'Events'. It's a partial implementation (specifically, I don't define 'self.handle_event' here. That's up to you.

    class Event(object):
      def __init__(self, config):
        self.host = 'localhost'
        self.port = '5672'
        self.vhost = '/'
        self.user = 'foo'
        self.exchange = 'myx'
        self.queue = 'myq'
        self.recv_routing_key = 'msgs4me'
        self.passwd = 'bar'
    
        self.connected = False 
        self.connect()
    
    
      def connect(self):
    
        credentials = pika.PlainCredentials(self.user, self.passwd)
    
        parameters = pika.ConnectionParameters(host = self.host,
                                             port = self.port,
                                             virtual_host = self.vhost,
                                             credentials = credentials)
    
        srs = pika.connection.SimpleReconnectionStrategy()
    
        logging.debug('Events: Connecting to AMQP Broker: %s:%i' % (self.host,
                                                                  self.port))
        self.connection = tornado_adapter.TornadoConnection(parameters,
                                                          wait_for_open = False,
                                                          reconnection_strategy = srs,
                                                          callback = self.on_connected)
    
      def on_connected(self):
    
        # Open the channel
        logging.debug("Events: Opening a channel")
        self.channel = self.connection.channel()
    
        # Declare our exchange
        logging.debug("Events: Declaring the %s exchange" %  self.exchange)
        self.channel.exchange_declare(exchange = self.exchange,
                                    type = "fanout",
                                    auto_delete = False,
                                    durable = True)
    
        # Declare our queue for this process
        logging.debug("Events: Declaring the %s queue" %  self.queue)
        self.channel.queue_declare(queue = self.queue,
                                 auto_delete = False,
                                 exclusive = False,
                                 durable = True)
    
    
        # Bind to the exchange
        self.channel.queue_bind(exchange = self.exchange,
                              queue = self.queue,
                              routing_key = self.recv_routing_key)
    
        self.channel.basic_consume(consumer = self.handle_event, queue = self.queue, no_ack = True)
    
        # We should be connected if we made it this far
        self.connected = True
    

    And then I put that in a file called 'events.py'. My RequestHandlers and any back end code all utilize a 'common.py' module that wraps code that's useful to both (my RequestHandlers don't call any amqp module methods directly -- same for db, cache, etc as well), so I define 'events=None' at the module level in common.py, and I instantiate the Event object kinda like this:

    import events
    
    def runapp(config):
        if myapp.common.events is None: 
           myapp.common.events = myapp.events.Event(config)
        logging.debug("MYAPP.COMMON.EVENTS: %s", myapp.common.events)
        http_server = tornado.httpserver.HTTPServer(app,
                                                xheaders=config['HTTPServer']['xheaders'],
                                                no_keep_alive=config['HTTPServer']['no_keep_alive'])
        http_server.listen(port) 
        main_loop = tornado.ioloop.IOLoop.instance()
        logging.debug("MAIN IOLOOP: %s", main_loop)
        main_loop.start()
    

    Happy new year :-D

    0 讨论(0)
  • 2021-02-06 07:47

    Someone has reported success in merging Tornado and Pika here. From what I can tell, it isn't as simple as just calling Pika from Tornado, since both libraries want to have their own event loops in charge.

    0 讨论(0)
提交回复
热议问题