Search code examples
python-3.xrabbitmqpython-pika

Python get variable value outside the function in RabbitMQ


Below is the function am using to get messages from producer.py

def callback(ch, method, properties, body):
result = body.decode()
resp = JSONEncoder().encode(result)
json_resp = json.loads(resp)
print(json_resp)
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.stop_consuming()

This prints out the expected the result, but what am looking for is to get variable json_resp outside of callback function for further processing


Solution

  • make that variable global or you can store it in any Database or file, you can also use Python Data Structures like Dictionary, Lists initialise this outer side of function and append the value accordingly.