py_trees_ros
py_trees_ros copied to clipboard
Option to Destroy Subscription in `terminate`
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 toFalse
(current behavior). - In
setup()
, we storeself.node
. Further, ifunsubscribe_on_terminate
isFalse
, we create the subscription. - In
initialise()
, ifunsubscribe_on_terminate
isTrue
, we create the subscription. - In
terminate()
, ifunsubscribe_on_terminate
isTrue
, we destroy the subscription. - In
shutdown()
, ifunsubscribe_on_terminate
isFalse
, 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.