在将数据写入websocket和influxdb时实时监视数据流的库
dataRT的Python项目详细描述
datart python库
关于
datart做什么?
datart是infloxdbclient的替代品,infloxdbclient将数据写入infloxdb,同时将点回送到websocket。这允许您在web浏览器或其他客户机中实时查看数据。
我需要这个做什么?
datart被设计用于infloxdbclient已经被使用,但是不断地查询infloxdb并不能提供对数据的足够的实时访问。通过创建一个客户端网页,您可以实时显示数据,同时记录infloxdb中的点以备日后检查。
如果我不想使用infloxdb怎么办?
没问题!
infloxdb不是使用datart的要求。如果influx_url
参数设置为None
,datart会将数据回送到websocket,但不会尝试将数据发送到infloxdb实例。
安装
Pypi
> pip install dataRT
设置工具
> python setup.py install
用法
单线程应用程序
如果资源受限,则可以使用CoreClient
对象。
强烈建议在使用此技术时使用gevent monkey-patch。
首先,导入CoreClient
:
fromdataRTimportCoreClient
然后创建一个客户端:
client=CoreClient(influx_url=None,debug=True)
CoreClient
接受这些参数,所有参数都是可选的:
Keyword | Default | Description |
---|---|---|
^{ | ^{ | Bind host for the WebSocket |
^{ | ^{ | Bind port for the WebSocket |
^{ | ^{ | URL for InfluxDB. Note: if this is set to ^{ |
^{ | ^{ | Port for InfluxDB |
^{ | ^{ | Username for InfluxDB |
^{ | ^{ | Password for InfluxDB |
^{ | ^{ | Database for InfluxDB |
^{ | ^{ | Base path for accessing WebSocket resources |
^{ | ^{ | Show debug messages |
定义一个“action”函数,然后启动datart循环。当datart在其维护循环中运行时,将不断调用此函数:
defaction(possible_arg,possible_kwarg=100):print(possible_arg)print(possible_kwarg)client.run_in_loop(action,'possible_arg',possible_kwarg=1000)
一旦调用{{CD25>},客户端会阻塞,直到调用{{CD26>}或程序退出。
可用的客户端功能:
Method | Arguments | Description |
---|---|---|
^{ | ^{ | Writes points to the WebSocket and InfluxDB instance. ^{ |
^{ | ^{ | Passes a query to the InfluxDBClient |
^{ | ^{ | Passes a ^{ |
^{ | ^{ | Blocks and serves the WebSocket forever |
^{ | ^{ | Starts the WebSocket server |
^{ | ^{ | Stops the WebSocket server |
^{ | ^{ | Blocks and serves the WebSocket forever, but calls the ^{ |
可用的客户端属性:
Property | Can be set? | Type | Description |
---|---|---|---|
^{ | No | ^{ | Returns ^{ |
^{ | No | ^{ | Returns the InfluxDBClient object to be used with more complex InfluxDB operations |
^{ | Yes | function | Function to call when the WebSocket receives a new message. Should accept 2 args for the WebSocket client and the message (string) |
^{ | Yes | function | Function to call when the WebSocket connects. Should accept 1 arg for the WebSocket client |
^{ | Yes | function | Function to call when the WebSocket disconnects. Should accept 1 arg for the WebSocket client |
多进程应用程序
如果您需要更高的可靠性,或者希望避免代码可能阻塞datart后台任务,请使用Client
对象。
首先,导入Client
:
fromdataRTimportClient
然后创建一个客户端:
client=Client(influx_url=None,debug=True)
Client
接受这些参数,所有参数都是可选的:
Keyword | Default | Description |
---|---|---|
^{ | ^{ | Bind host for the WebSocket |
^{ | ^{ | Bind port for the WebSocket |
^{ | ^{ | URL for InfluxDB. Note: if this is set to ^{ |
^{ | ^{ | Port for InfluxDB |
^{ | ^{ | Username for InfluxDB |
^{ | ^{ | Password for InfluxDB |
^{ | ^{ | Database for InfluxDB |
^{ | ^{ | Base path for accessing WebSocket resources |
^{ | ^{ | Show debug messages |
一旦定义了客户机,就可以启动它:
client.start()
一旦调用{{CD41}},客户端将在另一个进程中启动并继续服务,直到调用{{CD26>}或程序退出。
可用的客户端功能:
Method | Arguments | Description |
---|---|---|
^{ | ^{ | Writes points to the WebSocket and InfluxDB instance |
^{ | ^{ | Starts the WebSocket server |
^{ | ^{ | Stops the WebSocket server |
我的websocket在哪里?
websocket将自己与websocket路径上的client
路由相关联:ws:\\<host>:<port>\<ws route>\client
。
对于默认设置,这将是ws:\\0.0.0.0:8080\ws\client
示例
示例可以在examples目录中找到。
库使用
- gevent_example.py演示如何使用gevent将datart客户机集成到单线程应用程序中。(注意:使用此方法时,必须小心不要阻止进程为WebSocket连接运行维护,否则可能会遇到问题)
- multiprocess_example.py演示如何通过利用单独的进程使用datart客户机作为infloxdbclient的就地替换(注意:这会消耗更多的系统资源,如果您在资源受限的环境中工作,请使用gevent方法)
html/js
- view_data.html使用基本jquery将接收到的每一条数据写入表中的页面。
- plot_data.html使用Chart.js库实时将每个点绘制到线图上。
许可
这个项目是由麻省理工学院授权的。 不提供保修,使用风险自负。