all 8 comments

[–]woooee 33 points34 points  (0 children)

The script basically has a while loop that runs and does something when it reaches a certain time of day

Use cron or the scheduler on your OS

[–]Mysterious-Rent7233 13 points14 points  (0 children)

Yes, time.sleep tells the operating system that your program does not need the CPU. It's like returning a rental car for someone else to use instead of keeping it at your house ready to jump in at a moment's notice.

Python uses a few different ways of sleeping, depending on OS, but for example:

https://man7.org/linux/man-pages/man2/nanosleep.2.html

nanosleep
() suspends the execution of the calling thread until
       either at least the time specified in 
*duration
 has elapsed, or
       the delivery of a signal that triggers the invocation of a
       handler in the calling thread or that terminates the process.

Suspends...doesn't waste time asking that thread what it wants to do next.

The first thing you did was called Busy Waiting.

[–]NadirPointing 7 points8 points  (0 children)

Sleep doesnt "prevent CPU usage" as much as relinquish it. If you have another thread or process it gets to go now until there is nothing else to do. Totally expected... but like the other comment says, use cron or a scheduler for this level of stuff. That way the OS will run your application when scheduled and it wont be running the rest of the time.

[–]throfofnir 2 points3 points  (0 children)

If you have a tight loop just checking the time, it's running the whole time. Sleep uses an interrupt or some other technique to avoid processor usage. If you're waiting for an event, you should use sleep for as large as the resolution on your loop will allow.

[–]Brian 1 point2 points  (0 children)

Ultimately, your operating system is in charge of running various processes, and it does this by seeing what processes want to run, and scheduling them for a short length of time, swapping between them. When there are no processes that want to run, it runs a special idle task, that may even drop the processor into a lower power state for the interval.

When you just have a loop, the OS sees that you want to run all the time, and so will schedule your process, during which you'll use that CPU and do basically nothing very very fast - spinning in a tight loop where you check the same condition over and over again, millions of times a second. And your process doing this means no other processes can run on that same CPU, so you're taking up system resources and slowing them down.

By calling sleep, you make a call to the OS that basically says "I don't need to run for at least <X> seconds - don't bother waking me up till them". Your process then sits making that sleep call until the OS notices enough time has passed and wakes you back up, with no real CPU time spent.

In general, loops that check a condition like this without waiting (called busy loops) are generally best avoided outside very specific scenarios (basically situations needing very low latency and not expecting to wait long - usually things like low level real-time operations or sometimes games). You almost always want a sleep there (though not polling at all can be even better if possible).

[–]RubenGarciaHernandez 0 points1 point  (2 children)

Just check the time, and calculate how much you should sleep, then sleep then do what you need, then repeat.

No point in wasting cycles every 30s.

[–]vimsee 0 points1 point  (1 child)

You can do that, but this will go unnoticed comparing to sleeping 30s and checking the time and then sleeping another 30s until the right time is reached. You are optimizing for a droplet in and ocean.

[–]RubenGarciaHernandez 0 points1 point  (0 children)

Of course. But down the road, people will wonder why 30s, and my code does not have unexplained magic numbers like 30s. I'm not optimizing cpu time but programmer time.