22import sys
33import time
44
5- from influxdb_client_3 import InfluxDBClient3 , QueryApiOptionsBuilder , WriteOptions , write_client_options
5+ from influxdb_client_3 import InfluxDBClient3
66
77
88"""
99This example shows how to set query and write timeouts.
1010They can be set directly using arguments (write_timeout, query_timeout) in the client constructor.
1111They can also be overridden in write and query calls.
1212
13- To trigger timeout and deadline expired exceptions, reset the timeout values in the examples or
13+ To trigger timeout and deadline expired exceptions, reset the timeout values in the examples or
1414supply new values as command line parameters.
1515
16- Be sure to update the host, token a database values below, before running this example.
16+ Be sure to update the host, token a database values below, before running this example.
1717"""
1818
1919DEFAULT_WRITE_TIMEOUT = 30_000 # in milliseconds
2222DEFAULT_TOKEN = 'my-token'
2323DEFAULT_DATABASE = 'test-data'
2424
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- )
3325
34- def client_with_timeouts_via_options (w_to : int , q_to : int ) -> InfluxDBClient3 :
35- return InfluxDBClient3 (
26+ def main (w_to : int , q_to : int ) -> None :
27+ print (f"main { w_to } , { q_to } " )
28+ lp_data = "timeout_example,location=terra fVal=3.14,iVal=42i"
29+ with InfluxDBClient3 (
3630 host = DEFAULT_HOST ,
3731 token = DEFAULT_TOKEN ,
3832 database = DEFAULT_DATABASE ,
39- write_client_options = write_client_options (
40- write_options = WriteOptions (timeout = w_to )
41- ),
33+ write_timeout = w_to ,
4234 query_timeout = q_to
43- )
35+ ) as client :
36+
37+ try :
38+ # write with write timeout set in client
39+ client .write (record = lp_data )
40+ print ("First write OK." )
41+ time .sleep (1 )
42+
43+ # write overriding client internal write timeout
44+ # to force an exception try a _request_timeout value like 1 ms
45+ client .write (record = lp_data , _request_timeout = 9_000 )
46+ print ("Second write OK." )
47+ time .sleep (1 )
48+ except Exception as e :
49+ print (f"Got exception on write: { e } " )
50+
51+ sql = "SELECT * FROM timeout_example"
52+
53+ try :
54+ # query using query timeout set in client
55+ result = client .query (sql )
56+ print ("\n First query result\n " , result )
57+ time .sleep (1 )
58+
59+ # query overriding client internal query timeout
60+ # To force an exception try a timeout value like 0.001 seconds
61+ result = client .query (sql , timeout = 3.0 )
62+ print ("\n Second query result\n " , result )
63+ except Exception as e :
64+ print (f"Got exception on query: { e } " )
4465
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 ("\n First 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 ("\n Second query result\n " , result )
6866
6967if __name__ == "__main__" :
7068 w_ct = DEFAULT_WRITE_TIMEOUT
@@ -76,4 +74,3 @@ def main(w_to: int, q_to: int) -> None:
7674 q_ct = int (arg )
7775
7876 main (w_ct , q_ct )
79-
0 commit comments