akarneliuk / pygnmi

The pure Python implementation of the gNMI client.
https://training.karneliuk.com
BSD 3-Clause "New" or "Revised" License
126 stars 44 forks source link

Unable to catch errors with pygnmi subscribe2 #112

Open theblues25 opened 1 year ago

theblues25 commented 1 year ago

I am currently working on a Python code using Pygnmi with Subscribe2. The problem I am facing is that when there is any error, such as a network issue or a node reboot, the exception is not triggered, causing the subscription session to stop updating the information without any warning or error messages. This means that I have no way of knowing when to reconnect the session again and the updates from the subscription stop.

I have tried to look for solutions online, but I have not been able to find any relevant information on how to handle this issue. It seems Subscribe2 has running on own thread.

As a result of this issue, I am unable to receive updates from the subscribed device, and I have to manually restart the subscription session every time an error occurs. This is not only time-consuming, but it also makes the code unreliable and prone to errors.

while True: try: with gNMIclient(target=(deviceip, 57400), username=inputjson['username'], password=inputjson['password'],insecure=True) as gnmic:

Retrieve the information from the device

response = gnmic.subscribe2(subscribe=subscribe) nodename = inputjson['device'][deviceip]['systemname'] for update in response: grpcpath = f"/{update['upate']['prefix']}/{update['update']['update'][0]['path']}" pathname = inputjson['device'][deviceip][grpcpath] pathval = check_pathval_type(update['update']['update'][0]['val']) pathtimestamp = update['update']['timestamp'] with InfluxDBClient(url=influxdb2url, token=influxdb2token, org=influxdb2org) as influxclient: p = Point("Nokia_measurement") \ .tag("device", nodename) \ .field(pathname, pathval) \ .time(datetime.utcnow(), WritePrecision.MS) with influxclient.write_api(write_options=SYNCHRONOUS) as write_api: write_api.write(bucket=influxdb2bucket, record=p) except: traceback.print_exc() print(f'{deviceip} Error')

I would really appreciate any guidance or advice on how to address this issue and make the subscription session more reliable and error-resistant. Perhaps there is an option to check the status of the session and implement an automatic reconnection to the existing session when an error occurs. This would ensure that the subscription session stays connected and continues to receive updates, even in the case of network or device errors.

Additionally, if there are any best practices or recommended techniques for handling errors in Subscribe2, I would be grateful to learn about them. I am open to any suggestions that can help improve the reliability of my code and minimize the manual intervention required to handle errors.

Thank you in advance for your assistance in resolving this issue.