php PHP睡眠延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15414605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP sleep delay
提问by Yeak
In PHP, I want to put a number of second delay on each iteration of the loop.
在 PHP 中,我想在循环的每次迭代上放置一些秒延迟。
for ($i=0; $i <= 10; $i++) {
$file_exists=file_exists($location.$filename);
if($file_exists) {
break;
}
//sleep for 3 seconds
}
How can I do this?
我怎样才能做到这一点?
回答by mavili
Use PHP sleep()function. http://php.net/manual/en/function.sleep.phpThis stops execution of next loop for the given number of seconds. So something like this
使用PHP sleep()功能。http://php.net/manual/en/function.sleep.php这会在给定的秒数内停止执行下一个循环。所以像这样
for ($i=0; $i <= 10; $i++) {
$file_exists=file_exists($location.$filename);
if($file_exists) {
break;
}
sleep(3); // this should halt for 3 seconds for every loop
}
回答by Husman
I see what you are doing... your delaying a script to constantly check for a file on the filesystem (one that is being uploaded or being written by another script I assume). This is a BAD way to do it.
我明白你在做什么......你延迟了一个脚本来不断检查文件系统上的文件(一个正在上传或由我假设的另一个脚本编写的文件)。这是一个糟糕的方式来做到这一点。
- Your script will run slowly. Choking the server if several users are running that script.
- Your server may timeout for some users.
- HDD access is a costly resource.
- There are better ways to do this.
- 您的脚本将运行缓慢。如果多个用户正在运行该脚本,则会阻塞服务器。
- 对于某些用户,您的服务器可能会超时。
- HDD 访问是一种昂贵的资源。
- 有更好的方法来做到这一点。
You could use Ajax. And use a timeout to call your PHP script every few seconds. This will avoid the slow script loading. And also you can keep doing it constantly (the current for loop will only run for 33 seconds and then stop).
你可以使用 Ajax。并使用超时每隔几秒钟调用一次您的 PHP 脚本。这将避免缓慢的脚本加载。而且你也可以不断地这样做(当前的 for 循环只会运行 33 秒然后停止)。
You can use a database. In some cases database access is faster than HDD access. Especially with views and caching. The script creating the file/uploading the file can set a flag in a table (i.e. file_exists) and then you can have a script that checks that field in your database.
您可以使用数据库。在某些情况下,数据库访问比 HDD 访问更快。尤其是视图和缓存。创建文件/上传文件的脚本可以在表中设置一个标志(即 file_exists),然后您可以使用一个脚本来检查数据库中的该字段。
回答by devBinnooh
You can use sleep(3)which sleeps the thread for 3 seconds.
您可以使用sleep(3)which 使线程休眠 3 秒。
Correctionsleep method in php are in seconds.
回答by Nole
Hare are two ways to sleep php script for some period of time. When you have your code and want to pause script working for some time use these functions. In these examples the first part of codewill be done on script run and the second part of codewill be done but with time delay.
有两种方法可以让 php 脚本休眠一段时间。当您拥有代码并希望暂停脚本工作一段时间时,请使用这些功能。在这些示例中,第一部分代码将在脚本运行时完成,第二部分代码将完成但有时间延迟。
- Using sleep()function you can define sleep time in seconds.
- 使用sleep()函数,您可以以秒为单位定义睡眠时间。
Example:
例子:
echo "Message 1";
// The first part of code.
$timeInSeconds = 3;
sleep($timeInSeconds);
// The second part of code.
echo "Message 2";
This way it is possible to sleep php script for 3 seconds. Using this function you can sleep script for whole number (integer) of seconds.
这样就可以让 php 脚本休眠 3 秒。使用此功能,您可以使脚本休眠整数(整数)秒。
- Using usleep()function you can define sleep time in microseconds. This sleep time is convenient for intervals that require more precise time than one second.
- 使用usleep()函数,您可以以微秒为单位定义睡眠时间。对于需要比一秒更精确的时间的间隔,此睡眠时间很方便。
Example:
例子:
echo "Message 1";
// The first part of code.
$timeInMicroSeconds = 2487147;
usleep($timeInMicroSeconds);
// The second part of code.
echo "Message 2";
You can use this function if you want to sleep php for smaller time values than second (float). In this example I have put script to sleep for 2.487147 seconds.
如果您想让 php 睡眠的时间值小于秒(浮动),则可以使用此功能。在这个例子中,我让脚本休眠了 2.487147 秒。
回答by davidofadelaide
Have you considered using a PHP Daemon script using supervisorD. I use it in multiple tasks that are required to be running all the time.
您是否考虑过使用supervisorD使用 PHP 守护进程脚本。我在需要一直运行的多个任务中使用它。
The catch is making sure that each time you are running your script you check for memory resources. If its too high, stop the process and then let it restart itself up again.
问题是确保每次运行脚本时都检查内存资源。如果它太高,请停止该过程,然后让它再次重新启动。
I have successfully used this process to be always checking database records for tasks to process.
我已经成功地使用这个过程来始终检查要处理的任务的数据库记录。
It might be overkill but worth considering.
这可能有点矫枉过正,但值得考虑。

