¿How does the queue size and callback mechanism exactly work?
[Working in ROS noetic ubuntu 20.04] I've a topic publishing robot joints at 100 Hz and the following code for testing purposes:
from sensor_msgs.msg import JointState
import rospy
rospy.init_node("queue_size_testing", anonymous=True)
def callback(msg) -> None:
now = rospy.Time.now()
print(f"[ACTUAL] secs={now.secs} nsecs={now.nsecs} [READ]secs={msg.header.stamp.secs} nsecs={msg.header.stamp.nsecs}")
rospy.sleep(0.1)
rospy.Subscriber('/joint_states',JointState,callback)
rospy.spin()
I'm just subscribing to the topic and executing the callback at 10 Hz (obviously this code is not appropriate since it makes no sense to make the callback sleep, it is developed for testing purposes). So, this code is able to process 10 messages per second, even if 100 are coming from the publisher. As I have not defined a value for the subscriber's queue_size, it has taken its default value (infinite). I̶n̶ ̶t̶h̶i̶s̶ ̶w̶a̶y̶,̶ ̶I̶ ̶u̶n̶d̶e̶r̶s̶t̶a̶n̶d̶ ̶t̶h̶a̶t̶ ̶t̶h̶e̶ ̶s̶u̶b̶s̶c̶r̶i̶b̶e̶r̶ ̶q̶u̶e̶u̶e̶ ̶s̶h̶o̶u̶l̶d̶ ̶g̶r̶o̶w̶ ̶a̶t̶ ̶a̶ ̶r̶a̶t̶e̶ ̶o̶f̶ ̶9̶0̶ ̶m̶e̶s̶s̶a̶g̶e̶s̶ ̶p̶e̶r̶ ̶s̶e̶c̶o̶n̶d̶ ̶a̶n̶d̶ ̶s̶h̶o̶u̶l̶d̶ ̶d̶i̶s̶p̶l̶a̶y̶ ̶o̶n̶ ̶t̶h̶e̶ ̶s̶c̶r̶e̶e̶n̶ ̶a̶l̶l̶ ̶t̶h̶e̶ ̶m̶e̶s̶s̶a̶g̶e̶s̶ ̶r̶e̶c̶e̶i̶v̶e̶d̶ ̶(̶w̶i̶t̶h̶o̶u̶t̶ ̶a̶n̶y̶ ̶m̶s̶g̶s̶ ̶d̶r̶o̶p̶p̶i̶n̶g̶)̶,̶ ̶s̶i̶n̶c̶e̶ ̶i̶t̶ ̶r̶e̶s̶p̶e̶c̶t̶s̶ ̶t̶h̶e̶ ̶F̶I̶F̶O̶ ̶p̶h̶i̶l̶o̶s̶o̶p̶h̶y̶. (This doesn't happen as if the queue_size is 10 and there are 20 available in the socket stream, the oldest 10 are discarded, the queue_size name for the subscriber may be a little misleading for this reason.) Here is the actual output:
[ACTUAL] secs=308 nsecs=8000000 [READ]secs=297 nsecs=835000000
[ACTUAL] secs=308 nsecs=106000000 [READ]secs=308 nsecs=100000000
[ACTUAL] secs=308 nsecs=108000000 [READ]secs=297 nsecs=845000000
[ACTUAL] secs=308 nsecs=206000000 [READ]secs=308 nsecs=200000000
[ACTUAL] secs=308 nsecs=208000000 [READ]secs=297 nsecs=855000000
[ACTUAL] secs=308 nsecs=306000000 [READ]secs=308 nsecs=300000000
[ACTUAL] secs=308 nsecs=308000000 [READ]secs=297 nsecs=865000000
[ACTUAL] secs=308 nsecs=406000000 [READ]secs=308 nsecs=400000000
[ACTUAL] secs=308 nsecs=408000000 [READ]secs=297 nsecs=875000000
[ACTUAL] secs=308 nsecs=506000000 [READ]secs=308 nsecs=500000000
[ACTUAL] secs=308 nsecs=508000000 [READ]secs=297 nsecs=885000000
[ACTUAL] secs=308 nsecs=606000000 [READ]secs=308 nsecs=600000000
[ACTUAL] secs=308 nsecs ...