|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys |
| 3 | +import time |
| 4 | + |
| 5 | +from influxdb_client_3 import InfluxDBClient3, QueryApiOptionsBuilder, WriteOptions, write_client_options |
| 6 | + |
| 7 | + |
| 8 | +""" |
| 9 | +This example shows how to set query and write timeouts. |
| 10 | +They can be set directly using arguments (write_timeout, query_timeout) in the client constructor. |
| 11 | +They can also be overridden in write and query calls. |
| 12 | +
|
| 13 | +To trigger timeout and deadline expired exceptions, reset the timeout values in the examples or |
| 14 | +supply new values as command line parameters. |
| 15 | +
|
| 16 | +Be sure to update the host, token a database values below, before running this example. |
| 17 | +""" |
| 18 | + |
| 19 | +DEFAULT_WRITE_TIMEOUT = 30_000 # in milliseconds |
| 20 | +DEFAULT_QUERY_TIMEOUT = 120_000 # in milliseconds |
| 21 | +DEFAULT_HOST = 'http://localhost:8181' |
| 22 | +DEFAULT_TOKEN = 'my-token' |
| 23 | +DEFAULT_DATABASE = 'test-data' |
| 24 | + |
| 25 | +def client_with_timeouts_via_constructor(w_to: int, q_to: int) -> InfluxDBClient3: |
| 26 | + return InfluxDBClient3( |
| 27 | + host=DEFAULT_HOST, |
| 28 | + token=DEFAULT_TOKEN, |
| 29 | + database=DEFAULT_DATABASE, |
| 30 | + write_timeout=w_to, |
| 31 | + query_timeout=q_to |
| 32 | + ) |
| 33 | + |
| 34 | +def client_with_timeouts_via_options(w_to: int, q_to: int) -> InfluxDBClient3: |
| 35 | + return InfluxDBClient3( |
| 36 | + host=DEFAULT_HOST, |
| 37 | + token=DEFAULT_TOKEN, |
| 38 | + database=DEFAULT_DATABASE, |
| 39 | + write_client_options=write_client_options( |
| 40 | + write_options=WriteOptions(timeout=w_to) |
| 41 | + ), |
| 42 | + query_timeout=q_to |
| 43 | + ) |
| 44 | + |
| 45 | +def main(w_to: int, q_to: int) -> None: |
| 46 | + print(f"main {w_to}, {q_to}") |
| 47 | + lp_data = "timeout_example,location=terra fVal=3.14,iVal=42i" |
| 48 | + client1 = client_with_timeouts_via_constructor(w_to, q_to) |
| 49 | + |
| 50 | + # write with write timeout set in client |
| 51 | + client1.write(record=lp_data) |
| 52 | + time.sleep(1) |
| 53 | + |
| 54 | + # write overriding client internal write timeout |
| 55 | + client1.write(record=lp_data, _request_timeout=9_000) |
| 56 | + time.sleep(1) |
| 57 | + |
| 58 | + sql = "SELECT * FROM timeout_example" |
| 59 | + |
| 60 | + # query using query timeout set in client |
| 61 | + result = client1.query(sql) |
| 62 | + print("\nFirst query result\n", result) |
| 63 | + time.sleep(1) |
| 64 | + |
| 65 | + # query overriding client internal query timeout |
| 66 | + result = client1.query(sql, timeout=3.0) |
| 67 | + print("\nSecond query result\n", result) |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + w_ct = DEFAULT_WRITE_TIMEOUT |
| 71 | + q_ct = DEFAULT_QUERY_TIMEOUT |
| 72 | + for index, arg in enumerate(sys.argv): |
| 73 | + if index == 1: |
| 74 | + w_ct = int(arg) |
| 75 | + if index == 2: |
| 76 | + q_ct = int(arg) |
| 77 | + |
| 78 | + main(w_ct, q_ct) |
| 79 | + |
0 commit comments