class TornadoConsumer(object): """Non-blocking, Tornado ioloop based consumer""" def __init__(self, broker_url, exchange, exchange_type, queue, routing_key, durable=True, ssl=False, io_loop=None): self.io_loop = io_loop or ioloop.IoLoop.instance() self._conn = None self._callbacks = [] self._broker_url = broker_url self._ssl = ssl self._exchange = Exchange(exchange, exchange_type, durable=durable) self._queue = Queue(queue, exchange=self._exchange, routing_key=routing_key) def add_callback(self, callback): assert not self._conn self._callbacks.append(callback) def start(self): self._conn = Connection(self._broker_url, self._ssl) self._consumer = self._conn.Consumer(self._queue, callbacks=self._callbacks) self.io_loop.add_handler(self._conn.fileno(), self._handle_event) def stop(self): self.io_loop.remove_handler(self._conn.fileno()) self._consumer.close() self._conn.release() def join(self, *args, **kwargs): pass def _handle_event(self): self._conn.drain_nowait()