2. Getting Started

2.1. Installing RTI Connector for Python

There are two ways to obtain RTI Connector for Python. You can install it with pip:

$ pip install rticonnextdds_connector

And then run your Connector applications:

$ python my_connector_app.py

You can also clone the repository and run the examples directly without installing Connector:

$ git clone --recursive https://github.com/rticommunity/rticonnextdds-connector-py.git

In order to access the examples, clone the github repository.

2.2. Running the examples

The examples are in the examples/python directory of the RTI Connector for Python GitHub repository.

In the simple example, writer.py periodically publishes data for a Square topic, and reader.py subscribes to the topic and prints all the data samples it receives.

Run the reader as follows:

python examples/python/simple/reader.py

In another shell, run the writer:

python examples/python/simple/writer.py

This is what reader.py looks like:

import rticonnextdds_connector as rti

with rti.open_connector(
        config_name="MyParticipantLibrary::MySubParticipant",
        url=file_path + "/../ShapeExample.xml") as connector:

    input = connector.get_input("MySubscriber::MySquareReader")

    print("Waiting for publications...")
    input.wait_for_publications() # wait for at least one matching publication

    print("Waiting for data...")
    for i in range(1, 500):
        input.wait() # wait for data on this input
        input.take()
        for sample in input.samples.valid_data_iter:
            # You can get all the fields in a get_dictionary()
            data = sample.get_dictionary()
            x = data['x']
            y = data['y']

            # Or you can access the field individually
            size = sample.get_number("shapesize")
            color = sample.get_string("color")
            print("Received x: " + repr(x) + " y: " + repr(y) +
                  " size: " + repr(size) + " color: " + repr(color))

And this is writer.py:

import rticonnextdds_connector as rti

with rti.open_connector(
        config_name="MyParticipantLibrary::MyPubParticipant",
        url=file_path + "/../ShapeExample.xml") as connector:

    output = connector.get_output("MyPublisher::MySquareWriter")

    print("Waiting for subscriptions...")
    output.wait_for_subscriptions()

    print("Writing...")
    for i in range(1, 100):
        output.instance.set_number("x", i)
        output.instance.set_number("y", i*2)
        output.instance.set_number("shapesize", 30)
        output.instance.set_string("color", "BLUE")
        output.write()

        sleep(0.5) # Write at a rate of one sample every 0.5 seconds, for ex.

    print("Exiting...")
    output.wait() # Wait for all subscriptions to receive the data before exiting

You can run the reader and the writer in any order, and you can run multiple instances of each at the same time. You can also run any other DDS application that publishes or subscribes to the Square topic. For example, you can use RTI Shapes Demo.