# Looking for feedback on a ROS C++ & python init/shutdown wrapper

**URL:** https://discourse.openrobotics.org/t/looking-for-feedback-on-a-ros-c-python-init-shutdown-wrapper/19166
**Category:** ROS General
**Tags:** noetic
**Created:** [February 25, 2021, 7:35pm UTC](https://discourse.openrobotics.org/t/looking-for-feedback-on-a-ros-c-python-init-shutdown-wrapper/19166 "2021-02-25T19:35:12Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Peter\_Mitrano](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/peter_mitrano/32/6929_2.png) [@Peter\_Mitrano](https://discourse.openrobotics.org/u/Peter_Mitrano)
#### Post date: [February 25, 2021, 7:35pm UTC](https://discourse.openrobotics.org/t/looking-for-feedback-on-a-ros-c-python-init-shutdown-wrapper/19166/1 "2021-02-25T19:35:12Z")

</div>

Hey folks, I was hoping to get some feedback from others on a new bit of code I’ve written:

> <https://github.com/UM-ARM-Lab/arc_utilities/blob/master/src/arc_utilities/ros_init.py>

> <https://github.com/UM-ARM-Lab/arc_utilities/blob/master/src/roscpp_initializer.cpp>

The goal is to have clean and consistent startup & shutdown behavior for python nodes. The catch is that I want this to work when I am using bindings to C++ ROS code. Therefore, I need the C++ ros::initialize to be called, hence the little roscpp\_initializer you see here.

Do you think using a context manager is a good idea here? Do you have something similar to this in your code base? Thoughts on when this might fail/be a bad idea?

Thanks!

---

<div class="post-metadata">

### Author: ![rgov](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/rgov/32/9229_2.png) [@rgov](https://discourse.openrobotics.org/u/rgov)
#### Post date: [February 25, 2021, 8:07pm UTC](https://discourse.openrobotics.org/t/looking-for-feedback-on-a-ros-c-python-init-shutdown-wrapper/19166/2 "2021-02-25T20:07:01Z")

</div>

If the only use of `RosContext` is going to be through the `with_ros` function decorator, then why not simply make the decorator do:

```python
def wrapper(*args, **kwargs):
    rospy_and_cpp_init(name)
    try:
        func(*args, **kwargs)
    finally:
        shutdown()

```

Otherwise if there’s another use for `RosContext`, a more descriptive name like `RosNode` might be more readable:

```python
with RosNode("talker"):
    rospy.Publisher(...)

```

Is it possible to have multiple `RosContext`s executing in parallel, for instance in an `asyncio` or threaded program? If so, how does `rospy.Publisher` know which to associate with?

What about just having `rospy_and_cpp_init` use `atexit.register()` to shutdown the node cleanly when the program terminates?
