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. 30
      light-wave.py

30
light-wave.py

@ -29,6 +29,7 @@ ax.set_xticklabels(())
# Updates the line chart for factory1
def animate1(i, ys):
try:
f1_request = session.get(
"http://ritherdon.abbether.net/api/readings/latest/1", timeout=5)
f1_data = f1_request.json()
@ -36,11 +37,26 @@ def animate1(i, ys):
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):
try:
f2_request = session.get(
"http://ritherdon.abbether.net/api/readings/latest/2", timeout=5)
f2_data = f2_request.json()
@ -48,6 +64,20 @@ def animate2(i, ys):
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,