我正在使用python 3.7和confluent-kafka。
以下是我用来轮询kafka服务器并阅读消息的伪代码。
while True:
MSG = CONSUMER.poll(0.1)
if MSG is None:
CONSUMER.commit()
print('No msg')
continue
if MSG.error():
print("Consumer error: {}".format(MSG.error()))
CONSUMER.commit()
continue
try:
rawMsg = format(MSG.value().decode('utf-8'))
testmsg = json.loads(rawMsg)
except:
print('invalid json format msg')
CONSUMER.commit()
如果kafka服务器由于某种原因关闭或断开连接,我希望引发异常。
当前,如果发生上述情况,则while循环将继续运行而不会出现任何错误,并显示无消息。
如何获取异常或检查是否可以在循环中每次都连接kafka服务器(如果要进行一些检查,它应该是轻量级的)。