在将数据写入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接受这些参数,所有参数都是可选的:

KeywordDefaultDescription
^{}^{}Bind host for the WebSocket
^{}^{}Bind port for the WebSocket
^{}^{}URL for InfluxDB. Note: if this is set to ^{} no attempt to connect to an InfluxDB instance will be made
^{}^{}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>}或程序退出。

可用的客户端功能:

MethodArgumentsDescription
^{}^{}, ^{}, ^{}Writes points to the WebSocket and InfluxDB instance. ^{} are used for the WebSocket, all other args are passed to the InfluxDBClient
^{}^{}, ^{}Passes a query to the InfluxDBClient
^{}^{}, ^{}Passes a ^{} to the InfluxDBClient
^{}^{}Blocks and serves the WebSocket forever
^{}^{}Starts the WebSocket server
^{}^{}Stops the WebSocket server
^{}^{}, ^{}, ^{}Blocks and serves the WebSocket forever, but calls the ^{} function every loop with the provided ^{} and ^{}

可用的客户端属性:

PropertyCan be set?TypeDescription
^{}No^{}Returns ^{} if the WebSocket is currently being served
^{}No^{}Returns the InfluxDBClient object to be used with more complex InfluxDB operations
^{}YesfunctionFunction to call when the WebSocket receives a new message. Should accept 2 args for the WebSocket client and the message (string)
^{}YesfunctionFunction to call when the WebSocket connects. Should accept 1 arg for the WebSocket client
^{}YesfunctionFunction 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接受这些参数,所有参数都是可选的:

KeywordDefaultDescription
^{}^{}Bind host for the WebSocket
^{}^{}Bind port for the WebSocket
^{}^{}URL for InfluxDB. Note: if this is set to ^{} no attempt to connect to an InfluxDB instance will be made
^{}^{}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>}或程序退出。

可用的客户端功能:

MethodArgumentsDescription
^{}^{}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

许可

这个项目是由麻省理工学院授权的。 不提供保修,使用风险自负。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在Android中维护应用程序状态   javajavacc:如何指定在特定上下文中需要哪些令牌?   java为什么改型会在重新加载数据时设置以前的响应?   java如何将键转换为字符串,反之亦然   java JDOM解析器插入#固定手动属性   java按元素对XML数据排序?   java Android中有哪些哈希算法?   java为什么使用可选返回类型进行单元测试失败?   Gson和argonauts使用Gson将javascript数组转换为json字符串并转换为javapojo时遇到的问题。试图让我的结构正确   java中的空格   java SQLite高分,草率IndexOutofBounds Android   使用Spring OAUTH2的java Make客户端   netbeans如何在java中创建一个JPopupMenu,其中包含一个要复制文本的项