Do you want to model your DDS applications in Python? This article explains how to start using OpenSplice DDS with Python.
OpenSplice DDS comes with a Python DCPS API that provides Python classes allowing you model your DDS application using Python. The Python DCPS API is a native Python binding that supports full DDS functionality. The language binding consists of a Python interface and a C wrapper implementation of the C99 API (C API for DDS).
Using OpenSplice DDS with Python
Getting started with the Python language binding
In order to use the Python language binding you will need to install the following.
- Python 3.4 or later
- GCC 4.8 or later
- pip3 – you can install this using the command package “python3-pip” on most linux package managers.
- Python 3.5 or 3.6
- pip – this is usually included in Python for windows
- Visual Studio 14 (2015) C compiler or later
Once you have installed Python you need to install Cython version 0.27 or later. You can do this using the command “pip3 install Cython”
On windows you can run pip3 from a command prompt if you tick add to path when you install cython. If you did not do then you will find pip3 in the Scripts directory of your Python installation. This is usually under C:\Users\<username>\AppData\Local\Programs
Installing the Python DCPS API
Before using the Vortex OpenSplice DDS Python language binding you need to install Vortex OpenSplice.
Once you have done this you will find the Python DCPS API files in the $OSPL_HOME/tools/python where $OSPL_HOME is the path to the OpenSplice DDS installation directory.
There are two ways to installed the Python DCPS API
- From the Python wheel
- From the source
The method you use will depend on the OpenSplice version you are using. Some versions do not have the python wheel file included so you need to install from source.
Installing from the Python wheel
If your version of OpenSplice includes the python wheel you should see a file or files ending in .whl in the $OSPL_HOME/tools/python directory eg on windows dds-6.10.2p1-cp35-cp35m-win_amd64.whl.
There may be more than one file – the cp numbers indicate which python version it works with. For example, dds-6.10.2p1-cp35-cp35m-win_amd64.whl works with Python 3.5.
You can install the package using pip.
For example, executing the command will install the wheel package, which is targeted for python 3.4 64-bit Linux:
$pip3 install dds-6.9.0-cp34-cp34m-linux_x86_64.whl
Or for python 3.5 64-bit Windows:
pip install dds-6.9.0-cp35-cp35m-win_amd64.whl
Installing from source
Ensure that the environment is set up for using OpenSplice by running the release.com or release.bat file in the OpenSplice installation directory.
From the $OSPL_HOME/src directory run the command:
$python3 setup.py install
For more details on using the OpenSplice python language binding the Python DCPS API Guide is a good place to start. You can find this and all the other OpenSplice documentation on the OpenSplice documentation page.
The documentation is also included in the distribution. You will find it in the $OSPL_HOME/docs/pdf directory
The Python language binding comes with a number of examples to help you get started. You can find these in the $OSPL_HOME/tools/python/examples directory.
Inside the examples directory you will find a number of different files which the examples use to run. These are:
- py – These are runnable python scripts that allow you to run the example.
- xml – An XML file that contains one or more Quality of Service (QoS) profiles for DDS entities
- idl – An interface description language file used to define topic(s).
What do the different python examples do?
There are four different python examples provided with the OpenSplice distribution. These are:
- qos_example.py – An example that demonstrates how to specify QoS settings using Python DCPS APIs. Also shows how to use a waitset.
- example1.py – Dynamic generation of Python Topic classes using IDL file and name. Uses a topic with a sequence.
- example2.py – Dynamic generation of Python Topic classes using Enumeration and nested modules.
- example4.py – Demonstrates finding DDS topics over-the-wire. Find topics registered by other processes, and read and write samples to those topics.
How do I run the Python examples?
To run the examples first you need to set up the OpenSplice environment variables. In a command prompt go to the $OSPL_HOME directory.
- On windows: run release.bat
- On Linux: source release.com
Go to the $OSPL_HOME/tools/python/examples directory.
If you are using single process memory mode you can just run the example. If you are using shared memory mode you will need to start the OpenSplice daemon before running the example. You can do this using the command:
or starting it from the launcher.
Run an example python script using the command: