IntrepidBrit

callback_hackery()?

Jul 6th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. ''' Consumer '''
  2. import pika
  3.  
  4. class MegaIntrepidClass:
  5.     awesome_attribute = None
  6.  
  7.     def callback_check_url(self, ch, method, properties, body):
  8.         # Do something like the below
  9.         # self.hold_it_against_me(body)  # Obviously can't do this "as is".
  10.         print(" [x] Received %r" % body)
  11.         ch.basic_ack(delivery_tag=method.delivery_tag)
  12.  
  13.     def run(self):
  14.         connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
  15.         channel = connection.channel()
  16.         channel.queue_declare(queue='queue', durable=True)
  17.         channel.basic_consume(self.callback_check_url, queue='queue', no_ack=True)
  18.         channel.start_consuming()
  19.  
  20. if __name__ == "__main__":
  21.     mic = MegaIntrepidClass()
  22.     mic.run()
  23.  
  24. ''' Producer '''
  25. import pika
  26.  
  27. if __name__ == "__main__":
  28.     connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
  29.     channel = connection.channel()
  30.     channel.queue_declare(queue='queue', durable=True)
  31.     channel.basic_publish(exchange='', routing_key='queue', body='Hello World!')
  32.     print(" [x] Sent!")
  33.     connection.close()
Add Comment
Please, Sign In to add comment