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
def animate1(i, ys):
f1_request = session.get(
"http://ritherdon.abbether.net/api/readings/latest/1", timeout=5)
f1_data = f1_request.json()
f1_reading = f1_data.get("reading")
ys.append(f1_reading)
ys = ys1[-x_length:]
line1.set_ydata(ys)
return line1,
try:
f1_request = session.get(
"http://ritherdon.abbether.net/api/readings/latest/1", timeout=5)
f1_data = f1_request.json()
f1_reading = f1_data.get("reading")
ys.append(f1_reading)
ys = ys1[-x_length:]
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
def animate2(i, ys):
f2_request = session.get(
"http://ritherdon.abbether.net/api/readings/latest/2", timeout=5)
f2_data = f2_request.json()
f2_reading = f2_data.get("reading")
ys.append(f2_reading)
ys = ys2[-x_length:]
line2.set_ydata(ys)
return line2,
try:
f2_request = session.get(
"http://ritherdon.abbether.net/api/readings/latest/2", timeout=5)
f2_data = f2_request.json()
f2_reading = f2_data.get("reading")
ys.append(f2_reading)
ys = ys2[-x_length:]
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')