Last active
August 29, 2015 14:10
-
-
Save achanda/321ff2fb04555ea5281f to your computer and use it in GitHub Desktop.
Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pika | |
connection = pika.BlockingConnection(pika.ConnectionParameters( | |
host='localhost')) | |
channel = connection.channel() | |
channel.exchange_declare(exchange='test_header', type='headers') | |
channel.exchange_declare(exchange='test_direct', type='topic') | |
channel.exchange_bind(destination='test_header', source='test_direct', routing_key='*') | |
channel.queue_declare(queue='rpc_queue') | |
channel.queue_bind(queue='rpc_queue', exchange='test_direct') | |
def fib(n): | |
if n == 0: | |
return 0 | |
elif n == 1: | |
return 1 | |
else: | |
return fib(n-1) + fib(n-2) | |
def on_request(ch, method, props, body): | |
n = int(body) | |
print " [.] fib(%s)" % (n,) | |
response = fib(n) | |
ch.basic_publish(exchange='test_direct', | |
routing_key=props.reply_to, | |
properties=pika.BasicProperties(correlation_id = \ | |
props.correlation_id), | |
body=str(response)) | |
ch.basic_ack(delivery_tag = method.delivery_tag) | |
channel.basic_qos(prefetch_count=1) | |
channel.basic_consume(on_request, queue='rpc_queue') | |
print " [x] Awaiting RPC requests" | |
channel.start_consuming() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment