ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org

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

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

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!

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

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:

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

Is it possible to have multiple RosContexts 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?

1 Like