Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update get data to support parallel queries using ThreadPoolExecutor #31

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions ndbc_api/ndbc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
ResponseException, TimestampException)
from .utilities.req_handler import RequestHandler
from .utilities.singleton import Singleton
from concurrent.futures import ThreadPoolExecutor


class NdbcApi(metaclass=Singleton):
Expand Down Expand Up @@ -446,9 +447,10 @@ def get_data(
# accumulated_data records the handled response and parsed station_id
# as a tuple, with the data as the first value and the id as the second.
accumulated_data: List[Tuple[Union[pd.DataFrame, dict], str]] = []
for station_id, mode in itertools.product(handle_station_ids, handle_modes):
try:
data = self._handle_get_data(
with ThreadPoolExecutor(max_workers=len(handle_station_ids) * len(handle_modes)) as executor:
futures = [
executor.submit(
self._handle_get_data,
station_id=station_id,
mode=mode,
start_time=start_time,
Expand All @@ -457,11 +459,19 @@ def get_data(
as_df=as_df,
cols=cols
)
accumulated_data.append(data)
except (RequestException, ResponseException, HandlerException) as e:
self.log.error(f'Failed to process request for station_id {station_id} '
f'and mode {mode} with error: {e}')
continue
for station_id, mode in itertools.product(handle_station_ids, handle_modes)
]
for future in futures:
try:
data = future.result()
accumulated_data.append(data)
except (RequestException, ResponseException, HandlerException) as e:
self.log.error(
f"Failed to process request for station_id {station_id} "
f"and mode {mode} with error: {e}"
)
continue

# check that we have some response
if len(accumulated_data) == 0:
raise ResponseException(f'No data was returned for station_ids {handle_station_ids} '
Expand Down
Loading