Is there another difference between sleep and usleep? - php

Is the only difference between sleep function and usleep one Is that the parameter of first one is in seconds and the other one in micro seconds ?? Is there any difference else ??
Anther thing please, I'll use this functions with loops, Is there any problem will accident me on that??

From a review of the PHP documentation on usleep and that of sleep, you'll notice that there are 2 differences:
The argument for usleep is an integer representing micro seconds (a micro second is one millionth of a second), while the argument for sleep is an integer representing seconds.
sleep returns "zero on success, or FALSE on error." usleep has no return value. Here are more details on the return value for sleep:
If the call was interrupted by a signal, sleep() returns a non-zero
value. On Windows, this value will always be 192 (the value of the
WAIT_IO_COMPLETION constant within the Windows API). On other
platforms, the return value will be the number of seconds left to
sleep.
In general, you should be ok using these functions within loops. That being said, though, the more important questions to ask are: Why do you need a solution that depends on halting execution for a certain amount of time? Is this really a good solution to your problem, or is it a hack-fix for some strange bug or edge case that you just want to have go away?

usleep — Delay execution in microseconds.
Halt time in micro seconds. A micro second is one millionth of a second.
`sleep` — Delay execution
Delays the program execution for the given number of seconds.
i don't get more difference between this two function

sleep() allows your code to sleep in seconds.
sleep(5); // sleeps for 5 seconds
usleep() allows your code with respect to microseconds.
usleep(2500000); // slees for 2.5 seconds
Other than that, I think they're identical.
sleep($n) == usleep($n * 1000000)
usleep(25000) only sleeps for 0.025 seconds.

Related

How can I delay the execution time of a php script, so the python script can finish

I have a script in php that is calling a python one, which is a neural network being trained and it takes about 80 seconds to finish, but my php script finishes fast.
I tried just creating a csv file in python which is a quick process, and it worked, so its really a question of time.
I've already tried using the following code, and it didn't worked.
var_dump(time_sleep_until(microtime(true)+100));
Just use the Symphony Process component -> https://symfony.com/doc/current/components/process.html
It allows you to run processes asynchronously and check whether they are still running: https://symfony.com/doc/current/components/process.html#running-processes-asynchronously
Cheers.
Straight from the manual
https://www.php.net/manual/en/function.sleep.php
sleep ( int $seconds ) : int
Delays the program execution for the given number of seconds.
Parameters ¶
seconds
Halt time in seconds.
Return Values ¶
Returns zero on success, or FALSE on error.
If the call was interrupted by a signal, sleep() returns a non-zero value. On Windows, this value will always be 192 (the value of the WAIT_IO_COMPLETION constant within the Windows API). On other platforms, the return value will be the number of seconds left to sleep.
Errors/Exceptions ¶
If the specified number of seconds is negative, this function will generate a E_WARNING.
usleep(100); is more intuitive for what you are trying to do than using time_sleep_until, although I believe your time_sleep_until should work pretty similarly. However, the manual for time_sleep_until() seems to suggest that it may not work consistently for short times (such as .2 seconds). I would try usleep instead.

Falsely getting "The request was aborted because it exceeded the maximum execution time"

I say "falsely" because:
this script runs comparatively faster than other similar scripts in the same application and it uses an extremely similar structure (it takes about 5 seconds with breakpoints if click continue when debugging)
none of my other scripts time-out
almost all of my scripts contain something like this:
//give script enough time to run
$rowcount = count($rows);
set_time_limit(3 * $rowcount);
the bit that is timing-out is an execute() in a PDO wrapper class that I use on every page of my application
So my question is: what can cause this error if the maximum execution time is not being exceeded?
The answer is in my own mess.
I said "it takes about 5 seconds..." but I've only given 3 seconds per record.
With 1 record, the script times out because it only has 3 seconds...

What is the difference between sleep() and usleep() in this ob_flush example [duplicate]

This question already has answers here:
Difference among sleep() and usleep() in PHP
(5 answers)
Closed 8 years ago.
I am trying to learn the ob_start, ob_flush function and I found this code on internet
if (ob_get_level() == 0) ob_start();
for($i=0;$i<1000;$i++)
{
echo "$i<br />";
ob_flush();
flush();
usleep(30000);
// sleep(0.03);
}
I am trying to change the usleep() function which is use with microseconds to sleep (seconds) but it doesn't work as the usleep().
Is there something wrong or difference between sleep() and usleep()?
I think I have figured out an answer to what you are confused about :
sleep(0.03) does not work because it needs an integer. sleep(0.03) is interpreted as sleep(0).
Thats why there also is the function usleep() which provides sleep() in milliseconds, eg usleep(3000). And why there also is a third sleep function, time_nanosleep() which provides a mix of seconds and nanoseconds, even higher resolution.
I guess the three different functions is a result how the functions is built - demanding integers - and because the value of the largest integer is platform dependent. In 32bit the highest integer is 2147483647. In 64bit, the highest integer is 9223372036854775807.
The difference between sleep is sleep Delays the program execution for the given number of seconds and usleep Delays program execution for the given number of micro seconds.
You can't use fractions as a parameter for sleep()
sleep(0.03);
Here 0.03 will be cast into an integer
sleep(0);
So PHP will sleep for 0 seconds.
If you want to sleep for fractions of seconds, you have to use usleep() or time_nanosleep().

How to pause a script just for a fraction of a second in PHP using the SLEEP() function?

sleep(1); #waits/sleeps for one second then continue running the script
Q1. How to make this 1/100 of a second? which of these work: 0,01 or 0.01 or .01 ?
Q2. What are alternatives? wait(); or snap(); ?? how do they differ (more/less precise)?
Q1. How to make this 1/100 of a second? which of these work: 0,01 or 0.01 or .01 ?
None of the above!
usleep is what you want for fractions of a second. usleep(100000) will sleep for one tenth of one second.
Your other options are time_nanosleep which takes both seconds and freaking nanoseconds (one billion of which are one second), and time_sleep_until, which will sleep until a particular unix timestamp has been reached.
Be aware that your system might not have millisecond resolution, no less nanosecond resolution. You might have trouble sleeping for precisely tiny, tiny amounts of time.
Late answer... but you can use time_nanosleep(), i.e:
To sleep for 1/10 of a second (0.1):
time_nanosleep(0, 100000000);
To sleep for 1/100 of a second (0.01):
time_nanosleep(0, 10000000);
To sleep for 1/1000 of a second (0.001):
time_nanosleep(0, 1000000);
Use usleep in which you can pass in microseconds,
so for your case you can call usleep(10000) to sleep for 1/100 of a second.
What you are looking for is usleep() or time_nanosleep().
As for your second question, all these methods come with a high level of precision however I would advise you to test on your specific system if it's critical.

PHP: Time speed of functions, but it's too fast?

I've REALLY been wanting to test speeds of regex etc. and on php.net it has this example:
$time_start = microtime(true);
// Sleep for a while
usleep(100); // Or anything for that matter..
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
EDIT: What I meant was to play a large loop of functions in place of usleep(). It always shows a very random number that is always under 1. It shows nothing worth a benchmark!
Timers are like that. Wrap a loop around the function. Run it 10^6 times if you want to measure microseconds, 10^3 times if you want milliseconds. Don't expect to get more than 2 decimal digits of precision.
The usleep function need time in microsecond, you are specifying very low value, try increasing that:
usleep(2000000); // wait for two seconds
usleep function is also known to be tricky on Windows systems.
Or you can simply use the sleep function with seconds to be specified as its parameter eg:
sleep(10); // wait for 10 seconds
Cannot reproduce, and no one has a faster computer than me. 16 threads of execution, eight cores, i7 architecture. Your PHP build or OS must be suboptimal. This is running your code exactly.
`--> php tmp.php
Did nothing in 0.00011587142944336 seconds
.-(/tmp)---------------------------------------------------(pestilence#pandemic)-
This is with usleep(1000000)... (1 second)
`--> php tmp.php
Did nothing in 1.0000479221344 seconds
.-(/tmp)---------------------------------------------------(pestilence#pandemic)-
FYI, I was using PHP 5.3.0, not that it makes any difference (in this case) past 5.0.
Is your code taking less than a second to execute? If so, that'd explain your benchmarks. When I profile something for an article, I run my routine 10,000's of times... sometimes millions. Computers are fast these days.
As far as the "random" part goes, this is why I take several samples of large iterations and average them together. It doesn't hurt to throw the standard deviation in there too. Also, remember not to stream MP3s or play video while testing. :p

Categories