ThingsDB 模块构建器
项目描述
提莫
使用 Python 语言创建 ThingsDB 模块的库
安装
$ pip install py-timod
用法
ThingsDB 的模块stdin从stdout. 模块的工作必须是非阻塞的。
如果模块需要配置数据,例如连接字符串,则此配置将在启动后立即发送,但可能会在 ThingsDB 中更改模块配置时再次接收。
不要使用类似的函数,print因为这些函数将写入stdout,这是为 ThingsDB 保留的。相反,使用logging...to write tostderr代替。
以下代码可用作模板:(参见:https ://github.com/thingsdb/ThingsDB/tree/master/modules/python/demo.py )
import logging
from timod import start_module, TiHandler, LookupError
class Handler(TiHandler):
async def on_config(self, cfg):
logging.info(cfg)
async def on_request(self, req):
if 'message' not in req:
raise LookupError('missing `message` in request')
return req['message']
if __name__ == '__main__':
start_module('demo', Handler())