Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit f58d712

Browse files
committed
updates REST interactions to handle timeout
1 parent 0ca3d87 commit f58d712

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

google/cloud/bigquery/_pandas_helpers.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ def _row_iterator_page_to_arrow(page, column_names, arrow_types):
741741
return pyarrow.RecordBatch.from_arrays(arrays, names=column_names)
742742

743743

744-
def download_arrow_row_iterator(pages, bq_schema):
744+
def download_arrow_row_iterator(pages, bq_schema, timeout=None):
745745
"""Use HTTP JSON RowIterator to construct an iterable of RecordBatches.
746746
747747
Args:
@@ -752,6 +752,10 @@ def download_arrow_row_iterator(pages, bq_schema):
752752
Mapping[str, Any] \
753753
]]):
754754
A decription of the fields in result pages.
755+
timeout (Optional[float]):
756+
The number of seconds to wait for the underlying download to complete.
757+
If ``None``, wait indefinitely.
758+
755759
Yields:
756760
:class:`pyarrow.RecordBatch`
757761
The next page of records as a ``pyarrow`` record batch.
@@ -760,8 +764,16 @@ def download_arrow_row_iterator(pages, bq_schema):
760764
column_names = bq_to_arrow_schema(bq_schema) or [field.name for field in bq_schema]
761765
arrow_types = [bq_to_arrow_data_type(field) for field in bq_schema]
762766

763-
for page in pages:
764-
yield _row_iterator_page_to_arrow(page, column_names, arrow_types)
767+
if timeout is None:
768+
for page in pages:
769+
yield _row_iterator_page_to_arrow(page, column_names, arrow_types)
770+
else:
771+
start_time = time.monotonic()
772+
for page in pages:
773+
if time.monotonic() - start_time > timeout:
774+
raise concurrent.futures.TimeoutError()
775+
776+
yield _row_iterator_page_to_arrow(page, column_names, arrow_types)
765777

766778

767779
def _row_iterator_page_to_dataframe(page, column_names, dtypes):
@@ -779,7 +791,7 @@ def _row_iterator_page_to_dataframe(page, column_names, dtypes):
779791
return pandas.DataFrame(columns, columns=column_names)
780792

781793

782-
def download_dataframe_row_iterator(pages, bq_schema, dtypes):
794+
def download_dataframe_row_iterator(pages, bq_schema, dtypes, timeout=None):
783795
"""Use HTTP JSON RowIterator to construct a DataFrame.
784796
785797
Args:
@@ -793,14 +805,27 @@ def download_dataframe_row_iterator(pages, bq_schema, dtypes):
793805
dtypes(Mapping[str, numpy.dtype]):
794806
The types of columns in result data to hint construction of the
795807
resulting DataFrame. Not all column types have to be specified.
808+
timeout (Optional[float]):
809+
The number of seconds to wait for the underlying download to complete.
810+
If ``None``, wait indefinitely.
811+
796812
Yields:
797813
:class:`pandas.DataFrame`
798814
The next page of records as a ``pandas.DataFrame`` record batch.
799815
"""
800816
bq_schema = schema._to_schema_fields(bq_schema)
801817
column_names = [field.name for field in bq_schema]
802-
for page in pages:
803-
yield _row_iterator_page_to_dataframe(page, column_names, dtypes)
818+
819+
if timeout is None:
820+
for page in pages:
821+
yield _row_iterator_page_to_dataframe(page, column_names, dtypes)
822+
else:
823+
start_time = time.monotonic()
824+
for page in pages:
825+
if time.monotonic() - start_time > timeout:
826+
raise concurrent.futures.TimeoutError()
827+
828+
yield _row_iterator_page_to_dataframe(page, column_names, dtypes)
804829

805830

806831
def _bqstorage_page_to_arrow(page):

google/cloud/bigquery/table.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,10 @@ def to_arrow_iterable(
21522152
timeout=timeout,
21532153
)
21542154
tabledata_list_download = functools.partial(
2155-
_pandas_helpers.download_arrow_row_iterator, iter(self.pages), self.schema
2155+
_pandas_helpers.download_arrow_row_iterator,
2156+
iter(self.pages),
2157+
self.schema,
2158+
timeout=timeout,
21562159
)
21572160
return self._to_page_iterable(
21582161
bqstorage_download,
@@ -2366,6 +2369,7 @@ def to_dataframe_iterable(
23662369
iter(self.pages),
23672370
self.schema,
23682371
dtypes,
2372+
timeout=timeout,
23692373
)
23702374
return self._to_page_iterable(
23712375
bqstorage_download,

0 commit comments

Comments
 (0)