I want to go a little deeper into the "why" from a technical perspective. Not just you must do this, but why. I've looked at the rospy source code but I am still not sure about the following. Given this very common pattern:
rate = rospy.Rate(10)
while not rospy.is_shutdown():
# do stuff
rate.sleep()
Why is a rospy.spinOnce() not required? Or is it required? Because when I read the source of rate.sleep() didn't see that sleep include the spinOnce functionality which would imply that callbacks wouldn't be happening as a result of .sleep() or of is_shutdown(). But in fact callbacks do seem to be happening, so I must be misunderstanding a detail.
Refined restatement
Let me restate this in a very focused way. In python when must spin() used and when is spinOnce() used?
Originally posted by pitosalas on ROS Answers with karma: 628 on 2021-03-31
Post score: 0
Original comments
Comment by gvdhoorn on 2021-03-31:\
The initial two commenters contradict each other which shows that this is a tricky thing to really understand!
not really.
The answer is incorrect. @Delb's comment is correct.
This has also been discussed before here on ROS Answers.
roscpp
and rospy
just have different threading models.
Comment by miura on 2021-03-31:
@gvdhoorn Thank you for your comment.
I would like to revise my answer. Could you please let me know the deficiencies?
I would like to describe my perception. I was answering about "Why is a rospy.spinOnce() not required? Or is it required?" I understand that @Delb has supplemented about spin() in python.
Comment by gvdhoorn on 2021-03-31:
The question is specifically about rospy
.
You link to roscpp
tutorials and state the same "should apply to Python". But that's not correct.
roscpp
and rospy
handle these things differently.
@Delb already made that clear.
Comment by miura on 2021-03-31:
Thank you. I'm going to leave the answers and comments as they are now.
I thought about deleting the wrong comment, but I think it would make @Delb's comment a bit confusing.
If there is a good way to fix it, please let me know.