splintered-reality/py_trees_ros

Option to Destroy Subscription in `terminate`

Opened this issue · 0 comments

Currently, once you run setup on any behavior in py_trees_ros/subscribers.py, that subscription is active and accepting messages until the code terminates and it is garbage-cleaned. This means it stays on the wait set and consumes resources from the callback group, even at times we don't need it. For example, if we have a large tree and only want to subscribe to a topic in one particular behavior, we can destroy the subscription afterwards to save resources.

I'd propose the following implementation:

  • Pass a boolean flag, unsubscribe_on_terminate, to all subscription behaviors. Have it default to False (current behavior).
  • In setup(), we store self.node. Further, if unsubscribe_on_terminate is False, we create the subscription.
  • In initialise(), if unsubscribe_on_terminate is True, we create the subscription.
  • In terminate(), if unsubscribe_on_terminate is True, we destroy the subscription.
  • In shutdown(), if unsubscribe_on_terminate is False, we destroy the subscription.

Let me know what you think. We have certain subscribers that we only need at particular parts of the tree (e.g., only subscribe to object detection after the robot has moved to a pre-grasp pose) and we don't want it consuming callback resources during the rest of the tree.