ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

is there a safe method to stop a runtime subscriber

Yes: do it from outside the subscribe callback.

One approach is, In your main thread, create a "while loop", and inside the loop check a flag to determine if it should unregister the subscriber. This loop does not need to execute very often - it should spend most of its time sleeping (so you don't waste CPU cycles.) If your ros node shows 100% CPU usage, you did it wrong.

while not rospy.is_shutdown():
    if stop_my_sub:
        my_sub.unregister()
        my_sub = None
        stop_my_sub = False
    rospy.sleep(0.250)

However, it is usually a bad idea for a node to unregister a subscriber. It's easy to lose messages if the node frequently subscribes & unsubscribes a topic. A better design approach is to keep the subscriber active, and have it check a flag inside the callback to determine whether to ignore the new message or to process it.