提交 d7bc9edd 编写于 作者: D dingbo

docs: add python subscribe demo

上级 73ed6c9c
......@@ -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)
<TabItem label="Java" value="java">
<Java/>
</TabItem>
{/* <TabItem label="Python" value="Python">
<TabItem label="Python" value="Python">
<Python/>
</TabItem>
<TabItem label="Go" value="go">
{/* <TabItem label="Go" value="go">
<Go/>
</TabItem>
</TabItem> */}
<TabItem label="Rust" value="rust">
<Rust/>
</TabItem>
<TabItem label="Node.js" value="nodejs">
{/* <TabItem label="Node.js" value="nodejs">
<Node/>
</TabItem>
<TabItem label="C#" value="csharp">
......@@ -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
......
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'
......
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册