Hey all. Let's say I would like to periodically poll for a change in a variable/device/etc. Obviously I would like to use an infinite loop and when need be, break from the loop to end the polling routine. What I don't what to do is peak my processor usage by polling too often.

So my question is, what would be the most efficient way of slow down the execution of code within a loop? So far I've been using time.sleep() to slow down the execution time, but I'm not sure if this is the most efficient/elegant solution. Or perhaps there is there a way to allocate a certain amount of processing ability to a piece of code?

6 Years
Discussion Span
Last Post by convoluted

If I were you I would just stick with "time.sleep()". It's probably the easiest way and it's pretty damn accurate.

With regards to your opening paragraph: You wouldn't use an infinite loop, because you can't break from an infinite loop because then it would be a finite loop not an infinite loop...loop...infinite... :/


Well you can also place your loop in a function then use threading. With this you can manage the flow of your programme and CPU usage.

use modules sys,os,thread and time together.

Why am i saying this?... you can check with the CPU percentage or even give one core of the CPU to your application.

If The CPU reaches some level, You can halt the processing of your code.
I hope you get me. ;)


Awesome, thanks folks. Ha ha, up you're right, wouldn't technically be an infinite loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.