Package: rabbitmq-utils
rabbitmq-utilsThis package will provide easy connection to rabbitmq server.ProducerFollowing sample code will allow you to send the message to desire queue.InRabbitMQProducerclassPublisher Confirmsis implemented, So it will tell you weather the message is send to desire location or not.Note: In order to use default exchange, use "" as exchange name. When using the default exchange then routing key will be the name of queue.In order to make message persistent usepersistent_message=True. This will increase the disk size as well as latency. The message will be recovered even after the rabbitmq server is restarted.If you have priority base queue then you can passpriority=(some int)insendMessage.fromrabbitmq_utilsimportRabbitMQProducerimportjson# DEFINING MESSAGEmessage=json.dumps({'hello':'world'})# SENDINGrmqp=RabbitMQProducer(host='localhost',port=5672,virtual_host='/',username='guest',password='guest',exchange='test_exc',exchange_type='topic',persistent_message=False)is_sent=rmqp.sendMessage(message,routing_key='test_key')# RESULTifis_sent:print('INFO: Message sent.')else:print('ERROR: Unable to send on desire routing key.')ConsumerRabbitMQConsumerclass allow you define queue, define exchange and bind queue and exchange using routing key.Queue and Exchange are consider to be durable. If you are getting some error then remove the existing queue and exchange, before running this code.Callback function is called when message is received from the rabbitmq server. So define your callback function using following example:defmy_callback_function(ch,method,properties,body):# GETTING MESSAGEmessage=body.decode()# PERFORM YOUR LOGIC HEREmyLogic()# ACKNOWLEDGE WORK IS DONEch.basic_ack(delivery_tag=method.delivery_tag)returnNoneFollowing sample code will allow you to receive message from rabbitmq server.fromrabbitmq_utilsimportRabbitMQConsumer# STARTING RABBITMQ CONSUMERrmqc=RabbitMQConsumer(host='localhost',port=5672,virtual_host='/',username='guest',password='guest',queue_name='test_que',routing_key='test_key',exchange='test_exc',exchange_type='topic',callback_fun=my_callback_function,max_priority=2# Use this if you want priority base queue. (Default it is None))rmqc.receiveMessage()Remote Procedure Call (RPC)RPC allow to run a function on a remote computer and wait for the result. It is a synchronous call. This package provide simplest implementation of RPC.RPC consist of two parts. One is theserverthat will process the request and other isclientthat will generate the request to server. Following are example of RPC implementation.Serverfromrabbitmq_utils.rpcimportRPCServer# STARTING RPC SERVERserver=RPCServer(host='localhost',port=5672,virtual_host='/',username='guest',password='guest',queue_name='test_que',routing_key='test_key',exchange='test_exc',exchange_type='topic',callback_fun=rpc_callback_function)server.receiveMessage()Callback function of RPC is different from consumer callback function. In this callback we will return the result back to client.Note:resultmust be string. If it is not then usejson.dumps(result)to convert it to string.defrpc_callback_function(ch,method,properties,body):# GETTING MESSAGEmessage=body.decode()# PERFORM YOUR LOGIC HEREresult=myLogic()# RETURING RESPONSEch.basic_publish(exchange='',routing_key=properties.reply_to,properties=pika.BasicProperties(correlation_id=properties.correlation_id),body=result)# ACKNOWLEDGE WORK IS DONEch.basic_ack(delivery_tag=method.delivery_tag)returnNoneClientfromrabbitmq_utils.rpcimportRPCClientimportjson# DEFINING MESSAGEmessage=json.dumps({'hello':'world'})# SENDINGclient=RPCClient(host='localhost',port=5672,virtual_host='/',username='guest',password='guest',exchange='test_exc',exchange_type='topic',timeout=3,# wait 3 seconds for response. default is None (infinite wait).persistent_message=False)is_sent,response=client.sendMessage(message,routing_key='test_key',return_response=True)# OUTPUTprint(f'is_sent:{is_sent}\tcode:{client.getCode()}\tresponse:{response}')ClientsendMessagereceivereturn_responseargument (default=False). If this isTruethen client will wait for response for desiretimeoutperiod.  You can receive response later if you want by using follow sample code:# SEND REQUESTis_sent=client.sendMessage(message,routing_key)# PERFORM YOUR LOGIC# RECEIVE RESPONSEresponse=client.receiveResponse()Always check the validity of response using status code. Following code will help you check it:status_code=client.getCode()print(status_code)Code isinteger. Following table shows the meanings:CodeMeaning200Response is successfully obtained.408Timeout occur.AuthorTahir RafiqueReleasesDateVersionSummary17-Jan-24v1.4.1Adding exception handling in default callback function of consumer.13-Dec-23v1.4.0Adding queue priority in producer and consumer.14-Jul-23v1.3.0Adding persistent message option.14-Jul-23v1.2.1Correcting documentation.21-Jun-23v1.2.0Adding RPC to module.27-Apr-23v1.0.1Improving default callback function.27-Apr-23v1.0.0Initial build