Explain more about code

I see this code in the course:

def shutdownhook(self):
    # works better than the rospy.is_shutdown()
    self.ctrl_c = True

Could you explain the pros and cons between 2 ways to handle if shutdown happens?

Hello @NguyenDuyDuc, I hope the following explanation will make it clear for you.

Using rospy.is_shutdown():
Pros:

  • rospy.is_shutdown() is a method provided by ROS, and it’s considered the standard way to check if a ROS node should shut down. It’s part of the ROS API and is familiar to most ROS developers.
  • Graceful Shutdown: It allows for more graceful shutdowns, as it automatically handles signals and other mechanisms that indicate the intention to shut down the node.

Cons:

  • Blocking: If your code is in a long-running loop or performing a blocking operation, rospy.is_shutdown() may not be checked frequently enough, leading to a delay in detecting a shutdown signal.
  • External Signal Handling: It may not be suitable for scenarios where you need to handle shutdowns triggered by external signals (not just ROS).

Then lets come to self.ctrl_c flag.
Pros:

  • You have more control over the shutdown process and can implement custom logic before setting the shutdown flag. This can be useful if you need to perform specific actions or cleanup before shutting down.
  • Non-ROS Shutdown Signals: You can use this approach to handle shutdowns triggered by external signals or events beyond the scope of ROS.

Cons:

  • Manual Handling: You’re responsible for manually setting the shutdown flag (self.ctrl_c in this case), and it requires careful placement in your code to be effective.
  • Potential for Missed Signals: Depending on your code structure and how frequently you check the flag, there’s a risk of missing shutdown signals and not terminating the node promptly.

Regards,
Hamdi

1 Like

Thank you so much
It is very clear and helpful

You are welcome, I wish best of luck for you

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.