Skip to content

Commit

Permalink
Add timeout argument to all requests.get calls (#31)
Browse files Browse the repository at this point in the history
This avoids having the program potentially hanging endlessly
  • Loading branch information
mhhd2020 authored Sep 13, 2022
1 parent ba821da commit 6678ff3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
5 changes: 3 additions & 2 deletions analyses/modules/analysis_completeness.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from analyses.helpers import AnalysisResult, QualityLevel
from analyses.modules.analysis_base import Analysis

from constants import STATUS_UPDATES_ANALYSES, OHSOME_API
from constants import STATUS_UPDATES_ANALYSES, OHSOME_API, TIMEOUT_REQUESTS
from helper_modules.bbox_utils import Bbox
from helper_modules.progress import update_progress

Expand Down Expand Up @@ -101,7 +101,8 @@ def request(aggregation: str, **params: Any) -> requests.Response:
:return: Response from ohsome
"""
aggregation = aggregation.replace("density", "count/density")
return requests.get(f"{OHSOME_API}/elements/{aggregation}", params)
return requests.get(f"{OHSOME_API}/elements/{aggregation}", params,
timeout=TIMEOUT_REQUESTS)

def plot_results(self, data: List[Tuple[str, float]], title: str, y_label: str) -> None:
"""
Expand Down
6 changes: 3 additions & 3 deletions analyses/modules/analysis_landmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from analyses.modules.analysis_base import Analysis
from helper_modules.bbox_utils import Bbox
from helper_modules.progress import update_progress
from constants import STATUS_UPDATES_ANALYSES, OHSOME_API
from constants import STATUS_UPDATES_ANALYSES, OHSOME_API, TIMEOUT_REQUESTS


class LandmarkAnalysis(Analysis):
Expand Down Expand Up @@ -149,7 +149,7 @@ def add_density_for_tag(self, key: Optional[str], value: Optional[str],
filter_param += f" and {key}=*"
params.update({"filter": filter_param})

result = requests.get(OHSOME_API + url, params)
result = requests.get(OHSOME_API + url, params, timeout=TIMEOUT_REQUESTS)
result = json.loads(result.text)["result"][0]
self.density_sum += result["value"]
self.density[category] += result["value"]
Expand Down Expand Up @@ -179,7 +179,7 @@ def add_density_for_tag_aggregated(self, key: str, values_categories: Dict[str,
"groupByValues": ", ".join(values_categories.keys())
}

result = requests.get(OHSOME_API + url, params)
result = requests.get(OHSOME_API + url, params, timeout=TIMEOUT_REQUESTS)
result = json.loads(result.text)["groupByResult"]

for group_by_result in result:
Expand Down
7 changes: 4 additions & 3 deletions analyses/run_analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from helper_modules.progress import update_progress
from constants import (STATUS_UPDATES_ANALYSES, OHSOME_API,
STATUS_ERROR_OHSOME_NOT_AVAILABLE,
TIMEOUT_OHSOME_METADATA)
TIMEOUT_REQUESTS)


def run_for_single_bbox(bbox: Bbox,
Expand Down Expand Up @@ -60,7 +60,8 @@ def run_for_single_bbox(bbox: Bbox,
"time": time_str_whole_time
}

result = json.loads(requests.get(OHSOME_API + "/elementsFullHistory/bbox", params).text)
result = json.loads(requests.get(OHSOME_API + "/elementsFullHistory/bbox", params,
timeout=TIMEOUT_REQUESTS).text)
if "status" in result.keys() and result["status"] == 503:
update_progress(result_path=status_path, update=STATUS_ERROR_OHSOME_NOT_AVAILABLE)
return
Expand Down Expand Up @@ -121,7 +122,7 @@ def run_preparations_and_analyses(bboxes_input: List[Bbox], output_path: str) ->
:param output_path: The location where the analyses' output files will be saved
"""
try:
metadata = requests.get(OHSOME_API + "/metadata", timeout=TIMEOUT_OHSOME_METADATA).json()
metadata = requests.get(OHSOME_API + "/metadata", timeout=TIMEOUT_REQUESTS).json()
except requests.exceptions.ReadTimeout:
print("ERROR Timeout: Ohsome API not available")
for bbox in bboxes_input:
Expand Down
3 changes: 2 additions & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
TEMPLATE_ANALYSES = "analyses.html"
TEMPLATE_ANALYSES_RESULTS = "analyses_result.html"

TIMEOUT_OHSOME_METADATA = 50 # seconds
TIMEOUT_REQUESTS = 50 # seconds, see
# https://requests.readthedocs.io/en/stable/user/advanced/#timeouts

# Analyses: ---------------------------------------------------------------------------------------
STATUS_UPDATES_ANALYSES = MappingProxyType({
Expand Down

0 comments on commit 6678ff3

Please sign in to comment.