瀏覽代碼

iotdb 请求异常处理

heshixin 1 周之前
父節點
當前提交
7784c8cc5f
共有 1 個文件被更改,包括 12 次插入4 次删除
  1. 12 4
      iotdb_util.py

+ 12 - 4
iotdb_util.py

@@ -17,7 +17,7 @@ IotDbClient('127.0.0.1', username='root', password='root')
 # 请求数据
 dataset = IotDbClient.query('select * from root.unimat.dev_64')
 # 解析数据
-parse_dataset_by_time(dataset)
+parse_dataset_to_list(dataset)
 
 """
 
@@ -55,12 +55,16 @@ class IotDbClient:
                 self.__class__.session_pool = SessionPool(self._pool_config, pool_size, timeout_ms)
 
     @classmethod
-    def query(cls, sql) -> SessionDataSet:
-        session = IotDbClient.session_pool.get_session()
+    def query(cls, sql) -> SessionDataSet | None:
+        session = None
         try:
+            session = IotDbClient.session_pool.get_session()
             return session.execute_query_statement(sql)
+        except Exception as e:
+            return None
         finally:
-            IotDbClient.session_pool.put_back(session)
+            if session is not None:
+                IotDbClient.session_pool.put_back(session)
 
 
 def parse_dataset_to_list(dataset: SessionDataSet, timedela=600) -> dict:
@@ -71,6 +75,8 @@ def parse_dataset_to_list(dataset: SessionDataSet, timedela=600) -> dict:
     :param timedela: 对查询结果时间进行偏移
     :return:
     """
+    if dataset is None:
+        return {}
     column_names = dataset.column_names
     result = {}
     timed_list = []
@@ -102,6 +108,8 @@ def parse_last_data_set(dataset: SessionDataSet):
     :param dataset:
     :return:
     """
+    if dataset is None:
+        return {}
     result = {}
     while dataset.has_next():
         line = dataset.next()