[rospy] How do you unsubscribe to a topic, from within the callback ?
In a previous answer a similar issue was addressed but solved in another manner, however the solution suggsted:
def cb_once(msg, subscriber):
#do processing here
subscriber.unregister()
sub_once = None
sub_once = rospy.Subscriber('my_topic,', MyType, cb_once, sub_once)
However since you the right hand side of the last line has to finish before equating to the left hand side, thus sub_once point to wrong place, hence in the cb_once, subscriber.unregister() can't work. Is there another way?
Many Thanks
Guy
When you say "can't work" do you mean "I don't think it works" or "I tried it and it doesn't work"? The callback isn't called by Subscriber, although (I think that) there's a small chance that it will fire before Subscriber returns (very small, probably).
You can always wrap the callback code in a try block if you're worried that it will fail.