How is sleep using cpu resources?
Does it use 100% or 0% of cpu when you sleep your script. Can you explain?
As far as i know it increases load average, but how does it use CPU?
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.
The sleep() function delays execution of the current script for a specified number of seconds. Note: This function throws an error if the specified number of seconds is negative.
Yes, it is really true. Sleeping in a thread does reduce the CPU usage of that thread.
PHP is single-threaded, which means that it executes one instruction at a time before moving on. In other words, PHP naturally waits for a function to finish executing before it continues with the next statement.
The way that sleep
works is OS-dependent, because the PHP function sleep
calls into the appropriate runtime function to actually do what it says on the tin.
For Windows, that function is SleepEx
. For other operating systems it is the POSIX function sleep
. In both cases, the documentation for those functions clearly states that the sleeping thread is ineligible for running during the sleep period and therefore cannot consume CPU resources even if it wanted to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With