1
0
Fork 0
Browse Source

add exception handling to web requests.

This is just making the code more robust to timeouts and connection errors.
master
Craig Oates 3 years ago
parent
commit
17d7173a86
  1. 62
      light-wave.py

62
light-wave.py

@ -29,26 +29,56 @@ ax.set_xticklabels(())
# Updates the line chart for factory1 # Updates the line chart for factory1
def animate1(i, ys): def animate1(i, ys):
f1_request = session.get( try:
"http://ritherdon.abbether.net/api/readings/latest/1", timeout=5) f1_request = session.get(
f1_data = f1_request.json() "http://ritherdon.abbether.net/api/readings/latest/1", timeout=5)
f1_reading = f1_data.get("reading") f1_data = f1_request.json()
ys.append(f1_reading) f1_reading = f1_data.get("reading")
ys = ys1[-x_length:] ys.append(f1_reading)
line1.set_ydata(ys) ys = ys1[-x_length:]
return line1, line1.set_ydata(ys)
except requests.exceptions.ConnectionError:
pause = 60
time.sleep(60)
print(
f"[WARNING] MAX. REQUESTS EXCEEDED: Pausing requests for {pause} seconds...")
pass
except requests.exceptions.Timeout:
t_stamp = datetime.datetime.now()
print(f"[WARNING] TIMEOUT EXCEPTION: Request timed-out at {t_stamp}.")
time.sleep(60)
pass
except Exception as e:
print(f"[ERROR] GENERAL EXCEPTION: {e}")
finally:
return line1,
# Updates the line chart for factory2 # Updates the line chart for factory2
def animate2(i, ys): def animate2(i, ys):
f2_request = session.get( try:
"http://ritherdon.abbether.net/api/readings/latest/2", timeout=5) f2_request = session.get(
f2_data = f2_request.json() "http://ritherdon.abbether.net/api/readings/latest/2", timeout=5)
f2_reading = f2_data.get("reading") f2_data = f2_request.json()
ys.append(f2_reading) f2_reading = f2_data.get("reading")
ys = ys2[-x_length:] ys.append(f2_reading)
line2.set_ydata(ys) ys = ys2[-x_length:]
return line2, line2.set_ydata(ys)
except requests.exceptions.ConnectionError:
pause = 60
time.sleep(60)
print(
f"[WARNING] MAX. REQUESTS EXCEEDED: Pausing requests for {pause} seconds...")
pass
except requests.exceptions.Timeout:
t_stamp = datetime.datetime.now()
print(f"[WARNING] TIMEOUT EXCEPTION: Request timed-out at {t_stamp}.")
time.sleep(60)
pass
except Exception as e:
print(f"[ERROR] GENERAL EXCEPTION: {e}")
finally:
return line2,
# Starts the animations and shows the graph (I.E. 'main') # Starts the animations and shows the graph (I.E. 'main')