昨日想在python的一个函数中做一下发送次数的统计,需要用到全局变量,如下
1 COUNT = 0 2 3 def sendOneLineMsg(producer, listFromLine): 4 acSNStr = listFromLine[0] 5 macStr = listFromLine[1] 6 onlineTimeStr = listFromLine[2] 7 msg = {'clientMAC' : macStr, 'acSN' : acSNStr, 'onLineTime' : onlineTimeStr} 8 msgStr = json.dumps(msg) 9 producer.produce(msgStr) 10 COUNT += 1
结果报该错误,解决办法是在函数中加一行,global COUNT 即可解决