Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythons' sleep() CPU usage

How does Python handle its sleep() function? I want to create a clock which is ment to run like forever and print the time every 60 seconds and sleeps for the rest of the time. Does Python use CPU time while sleeping?

Thank you!

like image 643
John Python Avatar asked Jun 26 '13 19:06

John Python


People also ask

Is sleep CPU intensive?

Processes do not consume CPU resources while they are sleeping. They may add some overhead since the Kernel has to juggle them around, but that is very insignificant.

What does sleep () mean in python?

Python time sleep function is used to add delay in the execution of a program. We can use python sleep function to halt the execution of the program for given time in seconds.

How is time sleep implemented in python?

Adding a Python sleep() Call With time.sleep() Python has built-in support for putting your program to sleep. The time module has a function sleep() that you can use to suspend execution of the calling thread for however many seconds you specify.

Is python sleep busy waiting?

The sleep function is an OS call that differs from busy wait in that it doesn't block the thread.


1 Answers

No, sleep() does not use CPU time in python (or in any other programming language that I've heard of). Other alternatives for achieving similar results include sched-module, twisteds LoopingCall or GLibs Timeout.

Note that any method that includes waiting for a constant amount of time (like sleep()) will in the long run drift compared to an actual timeout API (like LoopingCall or GLib Timeout) unless you keep track of how much time has passed and change the sleep length on every cycle.

like image 153
Jussi Kukkonen Avatar answered Oct 05 '22 22:10

Jussi Kukkonen