ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Confused about spinOnce and sleep

asked 2021-03-31 08:45:36 -0500

pitosalas gravatar image

updated 2021-04-02 02:50:42 -0500

Delb gravatar image

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

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?

edit retag flag offensive close merge delete



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.

gvdhoorn gravatar image gvdhoorn  ( 2021-03-31 11:00:52 -0500 )edit

@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.

miura gravatar image miura  ( 2021-03-31 11:14:19 -0500 )edit

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.

gvdhoorn gravatar image gvdhoorn  ( 2021-03-31 11:16:57 -0500 )edit

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.

miura gravatar image miura  ( 2021-03-31 11:21:01 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2021-04-02 02:45:13 -0500

Delb gravatar image

updated 2021-04-02 07:28:27 -0500

gvdhoorn gravatar image

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.
edit flag offensive delete link more

answered 2021-03-31 09:33:10 -0500

miura gravatar image

updated 2021-03-31 09:33:24 -0500

The spinOnce is necessary for the callback function to work. Therefore, if a node only publishes, it can work without spinOnce.

edit flag offensive delete link more


It mentions what I answered, try searching spinOnce. This is a description of c++, but it should be the same for python.

miura gravatar image miura  ( 2021-03-31 09:36:22 -0500 )edit

No rospy and roscpp are different especially for the callbacks. From the same tutorial but for rospy :

Unlike roscpp, rospy.spin() does not affect the subscriber callback functions, as those have their own threads.

Delb gravatar image Delb  ( 2021-03-31 10:26:44 -0500 )edit

Despite the comments and responses it is still not crystal unclear. The tutorial uses raspy.spin(). I assume that's when you have nothing else to do and you just want to keep the node from exiting. And from the above comments it sounds like in python you never need spinOnce. I've added a new very specific pointed question above that restates what I was asking maybe in a more focused way.

pitosalas gravatar image pitosalas  ( 2021-04-01 14:12:20 -0500 )edit

Question Tools



Asked: 2021-03-31 08:45:36 -0500

Seen: 1,071 times

Last updated: Apr 02 '21