# How the rospy.sleep and how the rospy.Rate() works

**URL:** <https://get-help.theconstruct.ai/t/how-the-rospy-sleep-and-how-the-rospy-rate-works/10646>\
**Category:** General Support\
**Tags:** howto\
**Created:** [June 20, 2021, 4:14am UTC](https://get-help.theconstruct.ai/t/how-the-rospy-sleep-and-how-the-rospy-rate-works/10646 "2021-06-20T04:14:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![navjotsinghsodhi52](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/navjotsinghsodhi52/32/7141_2.png) [@navjotsinghsodhi52](https://get-help.theconstruct.ai/u/navjotsinghsodhi52)\
**Post date:** [June 20, 2021, 4:14am UTC](https://get-help.theconstruct.ai/t/how-the-rospy-sleep-and-how-the-rospy-rate-works/10646/1 "2021-06-20T04:14:45Z")

</div>

Hi, I would like to understand how exactly this rate.sleep() works in tandem with rospy.rate(). How these two are linked. I have attached the image and would like to know why we kept rate.sleep() inside the loop and what does it actually do. What if we increase the frequency in rospy.rate and how it affects our program. ![image](https://get-help.robotigniteacademy.com/uploads/default/original/2X/4/40a953df52c00136d2cff063a33331eb7c7e6bca.png) In short I want to understand the mechanism of working. Thanks.

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [June 21, 2021, 11:32am UTC](https://get-help.theconstruct.ai/t/how-the-rospy-sleep-and-how-the-rospy-rate-works/10646/2 "2021-06-21T11:32:44Z")

</div>

Please check the following post and let me know if it helps.

> **[Proper use of rospy.Rate or ros::Rate](https://get-help.theconstruct.ai/t/proper-use-of-rospy-rate-or-ros-rate/59)**
>
> Background I created this topic because I frequently come across the use of rospy.Rate (ROS Python) and ros::Rate (ROS C++) in places where they are not needed. By the way, what is this Rate thingy? According to ROS Wiki, rospy.Rate and ...

---

<div class="post-metadata">

**Author:** ![navjotsinghsodhi52](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/navjotsinghsodhi52/32/7141_2.png) [@navjotsinghsodhi52](https://get-help.theconstruct.ai/u/navjotsinghsodhi52)\
**Post date:** [June 22, 2021, 12:17pm UTC](https://get-help.theconstruct.ai/t/how-the-rospy-sleep-and-how-the-rospy-rate-works/10646/3 "2021-06-22T12:17:21Z")

</div>

This answer is fair enough for rate but I am still bit confused about how the rate.sleep works in a loop. For instance if we kept while i\< request.duration then then loop will continue until i becomes equal to request.duration but here my question is that rate.sleep() is inside the loop which means it is implemented every time the loop executes, then why does the robot stop when the i becomes equal to request.duration, is it like when the while loop becomes false, the rate.sleep() becomes true if I understood it correctly.

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [June 24, 2021, 9:05am UTC](https://get-help.theconstruct.ai/t/how-the-rospy-sleep-and-how-the-rospy-rate-works/10646/4 "2021-06-24T09:05:58Z")

</div>

This is the “password” for how `rospy.rate` works:

> > the Rate instance will attempt to keep the loop at 10hz (10 loops per second) by accounting for the time used by the work done during the loop. The `sleep()` method is used for implementing the set `Rate` value.

The `rate.sleep()` method (it is a method, not a variable) does not determine _when the loop ends_, only _how fast it runs_. Take for example:

```auto
rate = rospy.Rate(10) # 10Hz: 10 times per second
while condition:
   do_some_work()
   rate.sleep()

```

Let’s assume that the imaginary function `do_some_work()` takes `0.05` seconds to run. Without `rate.sleep()`, the loop would run 20 times per second (while `condition` evaluates to `True`).

With `rate.sleep()`, however, the loop will run 10 times per second, as specified in the definition of `rate`. The “enforcer” method, `rate.sleep()`, does this by making the code pause (sleep) for an additional `0.05` seconds so that the total time spent is `0.10` seconds instead of `0.05`.

**PS** : obviously we can only make the loop run slower than usual, not faster. If `do_some_work()` takes `0.20` seconds to run, the loop will run 5 times per second and the `rate` will be useless.

---

<div class="post-metadata">

**Author:** ![system](https://get-help.theconstruct.ai/uploads/default/original/2X/9/9d3ce8305cc0d0d9b6e07d15767a6f245c526f1b.jpeg) [@system](https://get-help.theconstruct.ai/u/system)\
**Post date:** [July 4, 2021, 9:06am UTC](https://get-help.theconstruct.ai/t/how-the-rospy-sleep-and-how-the-rospy-rate-works/10646/5 "2021-07-04T09:06:58Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
