0
$\begingroup$

Rosanswers logo

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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

To clearly answer your question and remove any doubt :

Why is a rospy.spinOnce() not required ?

  • Because it doesn't exist, I'm not sure why you would ask that given that there isn't any documentation about a function spinOnce in rospy. The first answer on this question provides interesting links.

But in fact callbacks do seem to be happening

  • That's the normal behavior with rospy. roscpp and rospy are not the same. When it comes to callbacks, with roscpp you need to use spin() or spinOnce() to process the events. Withrospy you don't need it because when you create a subscriber it gets its own thread.

In python when must spin() used ?

  • If you want to keep your node alive until you request a shutdown or ros crashes. You can use it even if you don't have any subscriber.

and when is spinOnce() used?

  • Never, it doesn't exist.

Originally posted by Delb with karma: 3907 on 2021-04-02

This answer was ACCEPTED on the original site

Post score: 3

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.