Python中获取变量值的功能之外的RabbitMQ

问题描述:

下面是功能正在使用摆脱producer.pyPython中获取变量值的功能之外的RabbitMQ

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() 

的消息,该打印出预期的结果,但是在找的是获得可变json_resp回调函数以外的进一步处理

使这个变量global或者你可以将其存储在任何数据库或文件中,还可以使用Python数据结构(如Dictionary,Lists)初始化该函数的外侧并相应地追加该值。

+0

存储在一个文件中。酷:) –

你可以在你的方法结束json_resp值。否则,你可以调用一个函数json_resp作为参数来执行你的进一步处理

1)

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() 
    return json_resp 


responce = callback(ch, method, properties, body) 
print responce 

2)

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() 
    my_process_func(json_resp) 

还可以治疗变种作为一个全局变量,如图在here, 我个人不太喜欢的东西

+0

可以发布样本代码 –

+0

刚刚在返回json_resp后的第一个点编辑答案 –

+0

。我试图访问外部函数像打印(json_resp)抛出和错误 - NameError:名称'json_resp'未定义。 –