Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvments to busy_wait #447

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

alexander-soare
Copy link
Collaborator

What this does

Makes busy_wait more precise for Linux and non-blocking on other platforms.
Renames busy_wait to precise_sleep, as it's not actually a "busy" wait.
Adds a benchmarking script to the if __name__ = "__main__" (I know this is unconventional, but it feels convenient and useful enough to break the rules for).

How it was tested

Ran the benchmarking snippet with all cores busy and found that precise_sleep was better.

How to checkout & try? (for the reviewer)

See the benchmarking snippet in the code.

# TODO(rcadene): find an alternative: from python 11, time.sleep is precise
end_time = time.perf_counter() + seconds
while time.perf_counter() < end_time:
def precise_sleep(seconds: float, blocking: bool = False):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this should default to blocking = True, and have the fancy non-cpu-blocking version for advanced users.

"""
end_time = time.perf_counter() + seconds
# 20 ms buffer is usually enough to account for an overly-long last sleep, even with heavy system load.
end_time_with_buffer = end_time - 0.02
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried increasing this buffer till I saw clear advantage in using precise_sleep instead of time.sleep with maximal system load. Please check on your end.

@alexander-soare alexander-soare self-assigned this Sep 20, 2024
@alexander-soare alexander-soare added ✨ Enhancement New feature or request 🌍 Real world Real-world robotics & controls labels Sep 20, 2024
@alexander-soare alexander-soare marked this pull request as draft September 20, 2024 11:59
@alexander-soare alexander-soare changed the title Minor improvments to busy_wait [don't review yet] Minor improvments to busy_wait Sep 20, 2024
@alexander-soare alexander-soare changed the title [don't review yet] Minor improvments to busy_wait Minor improvments to busy_wait Sep 20, 2024
@alexander-soare alexander-soare marked this pull request as ready for review September 20, 2024 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ Enhancement New feature or request 🌍 Real world Real-world robotics & controls
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant