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

Always add some form of sleep in while True loops #754

Open
dlech opened this issue Jul 31, 2020 · 1 comment
Open

Always add some form of sleep in while True loops #754

dlech opened this issue Jul 31, 2020 · 1 comment
Assignees

Comments

@dlech
Copy link
Member

dlech commented Jul 31, 2020

Running a while True loop without anything in the loop to make the thread sleep will cause Python to use 100% CPU.

Since ev3dev-stretch R3, this is especially noticeable since the user program is run with higher priority (nice -10) and it will starve the rest of the operating system that is running at a lower priority. This has side-effects like making the stop button in the ev3dev VS Code extension slow to respond.

The added sleep can be time.sleep() or anything that does a blocking wait for at least a millisecond or two (poll() with timeout, waiting for a sound to finish playing, etc.).

Example programs, such as this one from the README should be modified similar to this:

ts = TouchSensor()
leds = Leds()

print("Press the touch sensor to change the LED color!")

while True:
    if ts.is_pressed:
        leds.set_color("LEFT", "GREEN")
        leds.set_color("RIGHT", "GREEN")
    else:
        leds.set_color("LEFT", "RED")
        leds.set_color("RIGHT", "RED")
    # don't use 100% CPU
    time.sleep(0.01)
WasabiFan pushed a commit that referenced this issue Aug 1, 2020
add sleep in `while True` loop to avoid using 100% CPU

See #754
@WasabiFan
Copy link
Member

Thanks for the PR! I'll self-assign this to validate that the library itself has sleeps in similar loops.

@WasabiFan WasabiFan self-assigned this Aug 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants