Skip to content

Commit

Permalink
add error handling and code organization in multi threaded code
Browse files Browse the repository at this point in the history
  • Loading branch information
rajeshpandey2053 committed Apr 30, 2024
1 parent eb421a4 commit b101caf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
15 changes: 13 additions & 2 deletions pyQuARC/code/checker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json

from xmltodict import parse
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor, as_completed

from .custom_checker import CustomChecker
from .schema_validator import SchemaValidator
Expand Down Expand Up @@ -208,8 +208,9 @@ def _run_func(self, func, check, rule_id, metadata_content, result_dict):
self.metadata_format, {}
)
with ThreadPoolExecutor() as executor:
futures = []
for field_dict in list_of_fields_to_apply:
executor.submit(
future = executor.submit(
self._process_field,
func,
check,
Expand All @@ -219,6 +220,16 @@ def _run_func(self, func, check, rule_id, metadata_content, result_dict):
result_dict,
rule_mapping,
)
futures.append(future)

# Wait for all futures to complete
for future in as_completed(futures):
# Retrieve the result or raise an exception if an error occurred
try:
future.result()
except Exception as e:
# Handle the exception from the thread
raise e

def perform_custom_checks(self, metadata_content):
"""
Expand Down
25 changes: 14 additions & 11 deletions pyQuARC/code/custom_checker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from urllib.parse import urlparse
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor, as_completed


class CustomChecker:
Expand Down Expand Up @@ -181,16 +181,19 @@ def run(
future_results.append(future)

# Retrieve results from futures
for future in future_results:
func_return = future.result()
valid = func_return["valid"] # can be True, False or None
if valid is not None:
if valid:
validity = validity or (validity is None)
else:
if "value" in func_return:
invalid_values.append(func_return["value"])
validity = False
for future in as_completed(future_results):
try:
func_return = future.result()
valid = func_return["valid"] # can be True, False or None
if valid is not None:
if valid:
validity = validity or (validity is None)
else:
if "value" in func_return:
invalid_values.append(func_return["value"])
validity = False
except Exception as e:
raise e
result["valid"] = validity
result["value"] = invalid_values
return result

0 comments on commit b101caf

Please sign in to comment.