rabbitmq-tutorials/python/rpc_server.py
mrKazzila 336420b5ee
style: change double quotes to single quotes (where possible)
style: sort imports & format parameters onto separate lines for better clarity

Format all python files using `black`
2024-01-10 13:31:43 -08:00

41 lines
855 B
Python
Executable File

#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(
pika.ConnectionParameters(host="localhost"),
)
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):
n = int(body)
print(f" [.] fib({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(queue="rpc_queue", on_message_callback=on_request)
print(" [x] Awaiting RPC requests")
channel.start_consuming()