Skip to content

Instantly share code, notes, and snippets.

@fespino
Created August 11, 2015 00:31
Show Gist options
  • Save fespino/eae6d241e53d5a20aa3c to your computer and use it in GitHub Desktop.
Save fespino/eae6d241e53d5a20aa3c to your computer and use it in GitHub Desktop.

Revisions

  1. fespino created this gist Aug 11, 2015.
    35 changes: 35 additions & 0 deletions pika_blocking_consume.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import pika
    import pruebas.controller

    class BlockingConnectionAdapter(object):

    def __init__(self, controller, queue='test'):
    self.controller = controller
    self.connection = pika.BlockingConnection()
    self.channel = self.connection.channel()
    self.queue = self.channel.queue_declare(queue=queue, durable=True, exclusive=False, auto_delete=False)
    self.channel.basic_consume(self.on_message, queue=queue)

    def on_message(self, channel, method_frame, header_frame, body):
    print(method_frame.delivery_tag)
    print(body)
    print(channel.basic_ack(delivery_tag=method_frame.delivery_tag))
    self.controller.handle_request(body)

    def start_service(self):
    print('Starting...')
    try:
    self.channel.start_consuming()
    except KeyBoardInterrupt:
    self.channel.stop_consuming()
    self.teardown()
    self.connection.close()

    def teardown(self):
    print('Tearing down...')


    if __name__ == '__main__':
    controller = pruebas.controller.FakeController()
    adapter = BlockingConnectionAdapter(controller)
    adapter.start_service()