Last active
July 16, 2018 22:40
-
-
Save lukebakken/b1935b0ebae7b5c5a1762cd43e3a5bb7 to your computer and use it in GitHub Desktop.
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
*env*/ |
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 | |
import uuid | |
class FibonacciRpcClient(object): | |
def __init__(self): | |
self.user_id = 'guest' | |
self.password = 'guest' | |
creds = pika.credentials.PlainCredentials(self.user_id, self.password) | |
conn_params = pika.ConnectionParameters(credentials=creds, host='localhost') | |
self.connection = pika.BlockingConnection(conn_params) | |
self.channel = self.connection.channel() | |
result = self.channel.queue_declare(exclusive=True) | |
self.callback_queue = result.method.queue | |
self.channel.basic_consume(self.on_response, no_ack=True, | |
queue=self.callback_queue) | |
def on_response(self, ch, method, props, body): | |
if self.corr_id == props.correlation_id: | |
self.response = body | |
def call(self, n): | |
self.response = None | |
self.corr_id = str(uuid.uuid4()) | |
msg_properties = pika.BasicProperties( | |
reply_to=self.callback_queue, | |
correlation_id=self.corr_id, | |
user_id=self.user_id) | |
self.channel.basic_publish(exchange='', | |
routing_key='rpc_queue', | |
properties=msg_properties, | |
body=str(n)) | |
while self.response is None: | |
self.connection.process_data_events() | |
return int(self.response) | |
fibonacci_rpc = FibonacciRpcClient() | |
print(" [x] Requesting fib(30)") | |
response = fibonacci_rpc.call(30) | |
print(" [.] Got %r" % response) |
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 | |
conn_params = pika.ConnectionParameters(host='localhost') | |
connection = pika.BlockingConnection(conn_params) | |
channel = connection.channel() | |
channel.queue_declare(queue='rpc_queue') | |
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): | |
print("request user_id: %s" % props.user_id) | |
n = int(body) | |
print(" [.] fib(%s)" % n) | |
response = fib(n) | |
ch.basic_publish(exchange='', | |
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