Skip to content

Commit

Permalink
Added timeout handling to Python requests
Browse files Browse the repository at this point in the history
  • Loading branch information
itrujnara committed May 28, 2024
1 parent 43fa0b1 commit df4b8a6
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions bin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import Any

import requests
from requests.exceptions import RequestException

POLLING_INTERVAL = 0.5

Expand All @@ -16,7 +15,10 @@ def safe_get(url: str):
Get a URL and return the response.
"""
try:
return requests.get(url)
return requests.get(url, timeout = 300)
except requests.exceptions.Timeout as e:
print(f"Request timed out. This might be due to a server issue. If this persists, try again later. Details:\n{e}", file=sys.stderr)
sys.exit(9)
except requests.exceptions.RequestException as e:
print(f"A network issue occurred. Retrying request. Details:\n{e}", file=sys.stderr)
sys.exit(10)
Expand All @@ -27,7 +29,10 @@ def safe_post(url: str, data: dict = dict(), json: dict = dict()):
Post data to a URL and return the response.
"""
try:
return requests.post(url, data=data, json=json)
return requests.post(url, data = data, json = json, timeout = 300)
except requests.exceptions.Timeout as e:
print(f"Request timed out. This might be due to a server issue. If this persists, try again later. Details:\n{e}", file=sys.stderr)
sys.exit(9)
except requests.exceptions.RequestException as e:
print(f"A network issue occurred. Retrying request. Details:\n{e}", file=sys.stderr)
sys.exit(10)
Expand Down

0 comments on commit df4b8a6

Please sign in to comment.