Confused about spinOnce and sleep
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?
not really.
The answer is incorrect. @Delb's comment is correct.
This has also been discussed before here on ROS Answers.
roscpp
androspy
just have different threading models.@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.
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
androspy
handle these things differently.@Delb already made that clear.
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.