diff --git a/docs-cn/04-develop/06-subscribe.mdx b/docs-cn/04-develop/06-subscribe.mdx index d33a60d8de0a4552fa531d5e390c0faae22057fa..d471c114e827d7c4b40195c2c1b3c8f6a9d26ed4 100644 --- a/docs-cn/04-develop/06-subscribe.mdx +++ b/docs-cn/04-develop/06-subscribe.mdx @@ -160,7 +160,7 @@ make ./subscribe -sql='select * from meters where current > 10;' ``` -示例程序启动后,打开另一个终端窗口,启动 TDengine 的 shell 向 **D1001** 插入一条电流为 12A 的数据: +示例程序启动后,打开另一个终端窗口,启动 TDengine CLI 向 **D1001** 插入一条电流为 12A 的数据: ```sql $ taos @@ -172,7 +172,7 @@ $ taos ## 示例程序 -下面以一个示例程序介绍其具体使用方法。示例程序的目的是订阅数据库中所有电流超过 10A 的记录。 +下面的示例程序展示是如何使用连接器订阅所有电流超过 10A 的记录。 ### 准备数据 @@ -206,16 +206,16 @@ Query OK, 5 row(s) in set (0.004896s) - {/* + - + {/* - + */} - + {/* @@ -238,7 +238,7 @@ ts: 1597464600000 current: 10.3 voltage: 220 phase: 1 location: Beijing.Haidian ts: 1597465200000 current: 11.2 voltage: 220 phase: 1 location: Beijing.Haidian groupid : 2 ``` -接着,使用 taos 客户端向表中新增一条数据: +接着,使用 TDengine CLI 向表中新增一条数据: ``` # taos diff --git a/docs-examples/python/handle_exception.py b/docs-examples/python/handle_exception.py index 278feb4e96081e922555eab8ba8dfc1855909955..59554465c0891c80f0507656fcc79538c3811fa7 100644 --- a/docs-examples/python/handle_exception.py +++ b/docs-examples/python/handle_exception.py @@ -1,6 +1,5 @@ import taos -conn: taos.TaosConnection = None try: conn = taos.connect() conn.execute("CREATE TABLE 123") # wrong sql @@ -12,9 +11,6 @@ except taos.Error as e: except BaseException as other: print("exception occur") print(other) -finally: - if conn is not None: - conn.close() # output: # [0x0216]: syntax error near 'Incomplete SQL statement' diff --git a/docs-examples/python/subscribe_demo.py b/docs-examples/python/subscribe_demo.py index cc53e59d81a6bfd33d002f076a28016ece1a2b17..db9d49c3f4f8122634800c02a683d4cb022a7ba0 100644 --- a/docs-examples/python/subscribe_demo.py +++ b/docs-examples/python/subscribe_demo.py @@ -1 +1,38 @@ -import taos \ No newline at end of file +""" +Python asynchronous subscribe demo. +run on Linux system with: python3 subscribe_demo.py +""" + +from ctypes import c_void_p + +import taos +import time + + +def query_callback(p_sub, p_result, p_param, code): + """ + :param p_sub: pointer returned by native API -- taos_subscribe + :param p_result: pointer to native TAOS_RES + :param p_param: None + :param code: error code + :return: None + """ + print("in callback") + result = taos.TaosResult(c_void_p(p_result)) + # raise exception if error occur + result.check_error(code) + for row in result.rows_iter(): + print(row) + print(f"{result.row_count} rows consumed.") + + +if __name__ == '__main__': + conn = taos.connect() + restart = True + topic = "topic-meter-current-bg" + sql = "select * from power.meters where current > 10" # Error sql + interval = 2000 # consumption interval in microseconds. + _ = conn.subscribe(restart, topic, sql, interval, query_callback) + # Note: we received the return value as _ above, to avoid the TaosSubscription object to be deleted by gc. + while True: + time.sleep(10) # use Ctrl + C to interrupt