Advertisement
Guest User

Untitled

a guest
Jul 4th, 2014
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. import pika
  2. import sys
  3.  
  4.  
  5. connection = pika.BlockingConnection(pika.ConnectionParameters(
  6.     'localhost', credentials=pika.PlainCredentials('guest','guest')))
  7.  
  8. channel = connection.channel()
  9.  
  10. channel.queue_declare(queue='request')
  11. channel.queue_bind(queue='request', exchange='amq.topic')
  12.  
  13. def factorial(n):
  14.     if n == 0:
  15.         return 1
  16.     else :
  17.         return n * factorial (n -1)
  18.  
  19.  
  20. def on_request(ch, method, props, body):
  21.     n = int(body)
  22.  
  23.     print (" [.] factorial(%s)")  % (n,)
  24.     response = factorial(n)
  25.    
  26.     #Publish message after processing response | Client is subscribed to this queue.    
  27.     ch.basic_publish(exchange='',
  28.                      routing_key='mqtt-subscription-client123qos1',
  29.                      properties=pika.BasicProperties(correlation_id = \
  30.                                                      props.correlation_id),
  31.                      body=str(response))
  32.     ch.basic_ack(delivery_tag = method.delivery_tag)
  33.  
  34. channel.basic_qos(prefetch_count=1)
  35. channel.basic_consume(on_request, queue='request')
  36.  
  37. print (" Awaiting RPC requests")
  38. channel.start_consuming()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement