Skip to content

Commit f6731a7

Browse files
Takashi Nishibayashiparthea
Takashi Nishibayashi
authored andcommitted
ENH: Add timeout support (#76)
* Add timeout support * Fix a pep8 issue * Check before fetch results * Add test * timeoutMs -> timeout_ms * Add changelog
1 parent be301e2 commit f6731a7

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

docs/source/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Changelog
44
0.2.1 / 2017-??-??
55
------------------
66

7+
- :func:`read_gbq` now handles query configuration `query.timeoutMs` and stop waiting. (:issue:`76`)
8+
79
0.2.0 / 2017-07-24
810
------------------
911

pandas_gbq/gbq.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ class NotFoundException(ValueError):
172172
pass
173173

174174

175+
class QueryTimeout(ValueError):
176+
"""
177+
Raised when the query job timeout
178+
"""
179+
pass
180+
181+
175182
class StreamingInsertError(ValueError):
176183
"""
177184
Raised when BigQuery reports a streaming insert error.
@@ -536,6 +543,11 @@ def run_query(self, query, **kwargs):
536543

537544
while not query_reply.get('jobComplete', False):
538545
self.print_elapsed_seconds(' Elapsed', 's. Waiting...')
546+
547+
timeout_ms = job_config['query'].get('timeoutMs')
548+
if timeout_ms and timeout_ms < self.get_elapsed_seconds() * 1000:
549+
raise QueryTimeout('Query timeout: {} ms'.format(timeout_ms))
550+
539551
try:
540552
query_reply = job_collection.getQueryResults(
541553
projectId=job_reference['projectId'],

pandas_gbq/tests/test_gbq.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,19 @@ def test_configuration_raises_value_error_with_multiple_config(self):
884884
private_key=_get_private_key_path(),
885885
configuration=config)
886886

887+
def test_timeout_configuration(self):
888+
sql_statement = 'SELECT 1'
889+
config = {
890+
'query': {
891+
"timeoutMs": 1
892+
}
893+
}
894+
# Test that QueryTimeout error raises
895+
with pytest.raises(gbq.QueryTimeout):
896+
gbq.read_gbq(sql_statement, project_id=_get_project_id(),
897+
private_key=_get_private_key_path(),
898+
configuration=config)
899+
887900
def test_query_response_bytes(self):
888901
assert self.gbq_connector.sizeof_fmt(999) == "999.0 B"
889902
assert self.gbq_connector.sizeof_fmt(1024) == "1.0 KB"

0 commit comments

Comments
 (0)