Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the python bindings tutorial #450

Merged
merged 6 commits into from
Nov 3, 2023
Merged

Conversation

Voldivh
Copy link
Contributor

@Voldivh Voldivh commented Oct 3, 2023

🎉 New feature

Summary

This PR creates a new tutorial page with all the relevant information regarding the python API created for gz-transport in #411.

Checklist

  • Signed all commits for DCO
  • Added tests
  • Added example and/or tutorial
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

Signed-off-by: Voldivh <eloyabmfcv@gmail.com>
@Voldivh Voldivh requested a review from caguero as a code owner October 3, 2023 17:45
@github-actions github-actions bot added the 🎵 harmonic Gazebo Harmonic label Oct 3, 2023
@Voldivh Voldivh requested a review from azeey October 3, 2023 17:45
@@ -39,10 +38,11 @@ def main():
while True:
count += 1
vector3d_msg.x = count
if not (pub_stringmsg.publish(stringmsg_msg) or pub_vector3d.publish(vector3d_msg)):
if not (pub_stringmsg.publish(stringmsg_msg)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can drop the parens now that there is a single condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

print("Publishing 'Hello' on topic [{}]".format(stringmsg_topic))
if not (pub_vector3d.publish(vector3d_msg)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -20,5 +20,5 @@ combination of custom code and [ZeroMQ] (http://zeromq.org/).

## What programming language can I use with Gazebo Transport?

C++ is the native implementation and so far the only way to use the library.
We hope to offer different wrappers for the most popular languages in the future.
C++ is the native implementation and the only language that have available all library features.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
C++ is the native implementation and the only language that have available all library features.
C++ is the native implementation and the only language that has all available library features.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

C++ is the native implementation and so far the only way to use the library.
We hope to offer different wrappers for the most popular languages in the future.
C++ is the native implementation and the only language that have available all library features.
Python implementation is a wrapper around C++ methods using pybind11. It does not support all features like C++, but, contains the main features such as publication, subscription and service request.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Python implementation is a wrapper around C++ methods using pybind11. It does not support all features like C++, but, contains the main features such as publication, subscription and service request.
Python implementation is a wrapper around C++ methods using `pybind11`. It does not support all features like C++, but contains the main features such as publication, subscription and service request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


In this tutorial, we are going to show the functionalities implemented in python. This features are brought up to python by creating bindings from the C++ implementation using pybind11. It is important to note that not all of C++ features are available yet in python, on this tutorial we will go over the most relevant features we currently have on python. For more information refer to the [__init__.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/src/__init__.py) file which is a wrapper for all the bindings.

For this tutorial, we will create two nodes that are going to communicate via messages. One node will be a publisher that generates the information,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For this tutorial, we will create two nodes that are going to communicate via messages. One node will be a publisher that generates the information,
For this tutorial, we will create two nodes that communicate via messages. One node will be a publisher that generates the information,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks and done.


## Overview

In this tutorial, we are going to show the functionalities implemented in python. This features are brought up to python by creating bindings from the C++ implementation using pybind11. It is important to note that not all of C++ features are available yet in python, on this tutorial we will go over the most relevant features we currently have on python. For more information refer to the [__init__.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/src/__init__.py) file which is a wrapper for all the bindings.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shorten lines

Suggested change
In this tutorial, we are going to show the functionalities implemented in python. This features are brought up to python by creating bindings from the C++ implementation using pybind11. It is important to note that not all of C++ features are available yet in python, on this tutorial we will go over the most relevant features we currently have on python. For more information refer to the [__init__.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/src/__init__.py) file which is a wrapper for all the bindings.
In this tutorial, we are going to show the functionalities implemented in python. This features are brought up to python by creating bindings from the C++ implementation using pybind11. It is important to note that not all of C++ features are available yet in Python, on this tutorial we will go over the most relevant features we currently have on Python. For more information refer to the [__init__.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/src/__init__.py) file which is a wrapper for all the bindings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

from gz.transport13 import Node
```

The library `gz.transport13` contains all the Gazebo Transport elements that can be used in python. The final API we will use is contained inside the class `Node`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The library `gz.transport13` contains all the Gazebo Transport elements that can be used in python. The final API we will use is contained inside the class `Node`.
The library `gz.transport13` contains all the Gazebo Transport elements that can be used in Python. The final API we will use is contained inside the class `Node`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


First of all we declare a *Node* that will offer some of the transport
functionality. In our case, we are interested in publishing topic updates, so
the first step is to announce our topics names and their types. Once a topic name is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the first step is to announce our topics names and their types. Once a topic name is
the first step is to announce our topics names and their types. Once a topic name is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines +105 to +106
We need to register a function callback that will execute every time we receive
a new topic update. The signature of the callback is always similar to the ones
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
We need to register a function callback that will execute every time we receive
a new topic update. The signature of the callback is always similar to the ones
We need to register a function callback that will execute every time we receive
a new topic update. The signature of the callback is always similar to the ones

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


## Updating PYTHONPATH

If you made the binary installation of Gazebo Transport, you can skip this step and go directly to the next section. Otherwise, if you built the package from source it is needed to update the PYTHONPATH in order for python to recognize the library by doing the following:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you made the binary installation of Gazebo Transport, you can skip this step and go directly to the next section. Otherwise, if you built the package from source it is needed to update the PYTHONPATH in order for python to recognize the library by doing the following:
If you made the binary installation of Gazebo Transport, you can skip this step and go directly to the next section. Otherwise, if you built the package from source it is needed to update the PYTHONPATH in order for Python to recognize the library by doing the following:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

```{.sh}
python3 ./data_race_without_mutex.py
```
or
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
or
or

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 250 to 253
as an argument to the *advertise()* method.


## Subscribe Options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
as an argument to the *advertise()* method.
## Subscribe Options
as an argument to the *advertise()* method.
## Subscribe Options

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Signed-off-by: Voldivh <eloyabmfcv@gmail.com>
@Voldivh Voldivh requested a review from ahcorde October 5, 2023 16:01
Copy link
Contributor

@ahcorde ahcorde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signed-off-by: Voldivh <eloyabmfcv@gmail.com>
@Voldivh Voldivh requested a review from ahcorde October 6, 2023 14:17
@ahcorde
Copy link
Contributor

ahcorde commented Oct 16, 2023

friendly ping @Voldivh

@Voldivh
Copy link
Contributor Author

Voldivh commented Oct 16, 2023

friendly ping @Voldivh

The CI issues should have been solved by the last commit. Could you take a look @ahcorde?

@ahcorde
Copy link
Contributor

ahcorde commented Oct 16, 2023

@Voldivh there are still some unresolved comments

Signed-off-by: Voldivh <eloyabmfcv@gmail.com>
@Voldivh
Copy link
Contributor Author

Voldivh commented Oct 16, 2023

@Voldivh there are still some unresolved comments

Oh I completely missed that comment, sorry. Take a look now.

@codecov
Copy link

codecov bot commented Oct 16, 2023

Codecov Report

Merging #450 (0b273c1) into gz-transport13 (b012e82) will increase coverage by 0.01%.
The diff coverage is n/a.

❗ Current head 0b273c1 differs from pull request most recent head 46e050b. Consider uploading reports for the commit 46e050b to get more accurate results

@@                Coverage Diff                 @@
##           gz-transport13     #450      +/-   ##
==================================================
+ Coverage           87.78%   87.79%   +0.01%     
==================================================
  Files                  59       59              
  Lines                5696     5696              
==================================================
+ Hits                 5000     5001       +1     
+ Misses                696      695       -1     
Files Coverage Δ
python/src/transport/_gz_transport_pybind11.cc 75.00% <ø> (ø)

... and 2 files with indirect coverage changes


## Overview

In this tutorial, we are going to show the functionalities implemented in python. This features are brought up to python by creating bindings from the C++ implementation using pybind11. It is important to note that not all of C++ features are available yet in Python, on this tutorial we will go over the most relevant features we currently have on Python. For more information refer to the [__init__.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/src/__init__.py) file which is a wrapper for all the bindings.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In this tutorial, we are going to show the functionalities implemented in python. This features are brought up to python by creating bindings from the C++ implementation using pybind11. It is important to note that not all of C++ features are available yet in Python, on this tutorial we will go over the most relevant features we currently have on Python. For more information refer to the [__init__.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/src/__init__.py) file which is a wrapper for all the bindings.
In this tutorial, we are going to show the functionalities implemented in Python. This features are brought up to Python by creating bindings from the C++ implementation using pybind11. It is important to note that not all of C++ features are available yet in Python, on this tutorial we will go over the most relevant features we currently have on Python. For more information refer to the [__init__.py](https://github.com/gazebosim/gz-transport/blob/gz-transport13/python/src/__init__.py) file which is a wrapper for all the bindings.


## Running the examples

Open two new terminals and directly run the python scripts downloaded previously.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Open two new terminals and directly run the python scripts downloaded previously.
Open two new terminals and directly run the Python scripts downloaded previously.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still pending

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


## Service Responser

Unfortunately, this feature is not available on python at the moment. However, we can use a service responser created in C++ and make a request to it from a code in python. Taking that into account, we will use the [response.cc](https://github.com/gazebosim/gz-transport/blob/gz-transport13/example/responser.cc) file as the service responser.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Unfortunately, this feature is not available on python at the moment. However, we can use a service responser created in C++ and make a request to it from a code in python. Taking that into account, we will use the [response.cc](https://github.com/gazebosim/gz-transport/blob/gz-transport13/example/responser.cc) file as the service responser.
Unfortunately, this feature is not available on Python at the moment. However, we can use a service responser created in C++ and make a request to it from a code in Python. Taking that into account, we will use the [response.cc](https://github.com/gazebosim/gz-transport/blob/gz-transport13/example/responser.cc) file as the service responser.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still pending

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


## Running the examples

Open a new terminal and directly run the python script downloaded previously. It is expected that the service responser is running in another terminal for this, you can refer to the previous tutorial \ref services.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Open a new terminal and directly run the python script downloaded previously. It is expected that the service responser is running in another terminal for this, you can refer to the previous tutorial \ref services.
Open a new terminal and directly run the Python script downloaded previously. It is expected that the service responser is running in another terminal for this, you can refer to the previous tutorial \ref services.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still pending

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Received Vector3: [x: 4.0, y: 15.0, z: 20.0]
```
## Threading in Gazebo Transport
The way Gazebo Transport is implemented, it creates several threads each time a node, publisher, subscriber, etc is created. While this allows us to have a better paralization in processes, a downside is possible race conditions that might occur if the ownership/access of variables is shared across multiple processes. Even though python have it's [GIL](https://wiki.python.org/moin/GlobalInterpreterLock), all the available features are bindings created for it's C++ implementation, in other words, downsides commented before are still an issue to be careful. We recommend to always use threading locks when working with object that are used in several places (publisher and subscribers).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The way Gazebo Transport is implemented, it creates several threads each time a node, publisher, subscriber, etc is created. While this allows us to have a better paralization in processes, a downside is possible race conditions that might occur if the ownership/access of variables is shared across multiple processes. Even though python have it's [GIL](https://wiki.python.org/moin/GlobalInterpreterLock), all the available features are bindings created for it's C++ implementation, in other words, downsides commented before are still an issue to be careful. We recommend to always use threading locks when working with object that are used in several places (publisher and subscribers).
The way Gazebo Transport is implemented, it creates several threads each time a node, publisher, subscriber, etc is created. While this allows us to have a better paralization in processes, a downside is possible race conditions that might occur if the ownership/access of variables is shared across multiple processes. Even though Python have it's [GIL](https://wiki.python.org/moin/GlobalInterpreterLock), all the available features are bindings created for it's C++ implementation, in other words, downsides commented before are still an issue to be careful. We recommend to always use threading locks when working with object that are used in several places (publisher and subscribers).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Signed-off-by: Voldivh <eloyabmfcv@gmail.com>
@ahcorde
Copy link
Contributor

ahcorde commented Oct 16, 2023

Thank you @Voldivh

tutorials/06_python_support.md Outdated Show resolved Hide resolved
tutorials/06_python_support.md Outdated Show resolved Hide resolved
the first step is to announce our topics names and their types. Once a topic name is
advertised, we can start publishing periodic messages using the publishers objects.

```{.py}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw you had the directives #! [complete] in the example code. Would it be possible to do the same for these, so we don't have to duplicate the example code here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean for each or the small parts during the Walkthrough? I did it this way just to keep consistency with it is done on the other tutorials.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll approve as is, but I think it's better to use the doxygen trick instead of duplicating the code. The other tutorials should also be doing the same.

tutorials/06_python_support.md Show resolved Hide resolved
tutorials/06_python_support.md Outdated Show resolved Hide resolved
tutorials/06_python_support.md Outdated Show resolved Hide resolved
tutorials/06_python_support.md Outdated Show resolved Hide resolved
Signed-off-by: Voldivh <eloyabmfcv@gmail.com>
@Voldivh Voldivh requested a review from azeey October 25, 2023 20:18
@ahcorde
Copy link
Contributor

ahcorde commented Oct 31, 2023

friendly ping @azeey

@Voldivh Voldivh merged commit 6c4b1ec into gz-transport13 Nov 3, 2023
10 of 12 checks passed
@Voldivh Voldivh deleted the voldivh/python_tutorial branch November 3, 2023 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎵 harmonic Gazebo Harmonic
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants