Skip to content

Commit

Permalink
Use a with block for ThreadPoolExecutor (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmwinther authored Mar 14, 2024
1 parent 4f61720 commit ca06944
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/datadoc/backend/external_sources/external_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def __init__(self) -> None:
Initializes the future object.
"""
self.future: concurrent.futures.Future[T | None] | None = None
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
self.future = executor.submit(
self._fetch_data_from_external_source,
)
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
self.future = executor.submit(
self._fetch_data_from_external_source,
)

def wait_for_external_result(self) -> None:
"""Waits for the thread responsible for loading the external request to finish."""
Expand Down
5 changes: 3 additions & 2 deletions src/datadoc/backend/statistic_subject_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ def _parse_statistic_subject_structure_xml(
@property
def primary_subjects(self) -> list[PrimarySubject]:
"""Getter for primary subjects."""
self._parse_xml_if_loaded()
logger.debug("Got %s primary subjects", len(self._primary_subjects))
if not self._primary_subjects:
self._parse_xml_if_loaded()
logger.debug("Got %s primary subjects", len(self._primary_subjects))
return self._primary_subjects

def _parse_xml_if_loaded(self) -> bool:
Expand Down

0 comments on commit ca06944

Please sign in to comment.