Subscribing
The functionality to subscribe to an RT data stream is provided by the kxi-rtpy
package, which is an optional dependency of kxi-python
. If you want to use this feature, make sure to install all dependencies when installing kxi-python
using pip
. See the installation instructions for more details.
Example
This example shows you how to create a python subscriber for kdb Insights or kdb Insights Enterprise instance.
First, import the Subscriber
class.
>>> from rtpy import Subscriber
>>> import pykx as kx
>>> def on_message(position, buffer):
... print(f"position: {position}")
... obj = kx.deserialize(buffer)
... print(obj)
...
Subscriber
class implements the context manager protocol; this means it can be be used within a with
statement, and resource management is handled automatically.
>>> import os
>>> import signal
>>> with Subscriber(config_url=os.environ['KXI_CONFIG_URL'], on_message=on_message):
... print("Running... Press Ctrl+C to stop.")
... signal.pause()
...
config_url
can be either a kdb Insights Enterprise client URL or the path to a local JSON file (using the file URI scheme) containing the RT configuration. See the RT getting started guide for more details.
For more details on the Subscriber
class, see the kxi-rtpy
documentation.