subscriber callback in a python class is overwriting the class variables with the most recent data before the first callback processing finishes.
I am on ROS Melodic. When a subscriber is receiving data at a faster rate than it could process, the class variables are being updated before the callback process is finished. For example, when I run below subscriber. When the first callback is triggered tracker is set to 1 and after 5 seconds it was supposed to print 1, now before the completion of these 5 seconds, the subscriber receives another data, the call back will update tracker to 2. Now, this value 2 is printed twice instead of 1 and then 2. I am trying to say that the tracker is being set to most recent callback value before even the print process has been finished.
class listener:
def printer(self):
rospy.sleep(5)
print (self.tracker)
print("Look Above for value")
def callback(self, data):
rospy.loginfo("I heard %s", data)
self.tracker = self.tracker + 1
self.printer()
def __init__(self):
rospy.init_node('listener', anonymous=True)
self.tracker = 0
rospy.Subscriber('/path_query', PathQuery, self.callback,
queue_size=1)
rospy.spin()
if __name__ == '__main__':
listener()