php php永无止境的循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25678062/
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 never ending loop
提问by alagu
i am new to php, thanks you for your time :)
我是 php 新手,谢谢你的时间:)
i need an function that excutes by it self in php whitout the help of corn.for that i have comeup with the following code which works for me well but as it is an never ending loop will it cause any problem to my server or script, if so give me some suggestion or alternatives. thanks.
我需要一个可以在 php 中自行执行的函数,无需玉米的帮助。为此,我想出了以下代码,它对我很有效,但由于它是一个永无止境的循环,它会不会对我的服务器或脚本造成任何问题,如果是这样,请给我一些建议或替代方案。谢谢。
$interval=60; //minutes
set_time_limit(0);
while (1){
$now=time();
#do the routine job, trigger a php function and what not.
sleep($interval*60-(time()-$now));
}
回答by Hyder B.
We have used the infinite loop in a live system environment to basically wait for incoming SMS and then process it. We found out that doing it this way makes the server resource intensive over time and had to restart the server in order to free up memory.
我们在实时系统环境中使用了无限循环,基本上是等待传入的短信然后处理它。我们发现这样做会使服务器资源随着时间的推移而变得密集,并且不得不重新启动服务器以释放内存。
Another issue we encountered is when you execute a script with an infinite loop in your browser, even if you hit the stop button it will continue to run unless you restart Apache.
我们遇到的另一个问题是,当您在浏览器中无限循环地执行脚本时,即使您按下停止按钮,它也会继续运行,除非您重新启动 Apache。
while (1){ //infinite loop
// write code to insert text to a file
// The file size will still continue to grow
//even when you click 'stop' in your browser.
}
The solution is to run the PHP script as a deamon on the command line. Here's how:
解决方案是在命令行上将 PHP 脚本作为守护程序运行。就是这样:
nohup php myscript.php &
nohup php myscript.php &
the &
puts your process in the background.
在&
把你的程序在后台运行。
Not only we found this method to be less memory intensive but you can also kill it without restarting apache by running the following command :
我们不仅发现这种方法占用的内存更少,而且您还可以通过运行以下命令在不重新启动 apache 的情况下终止它:
kill processid
kill processid
Edit: As Dagon pointed out, this is not really the true way of running PHP as a 'Daemon' but using the nohup
command can be considered as the poor man's way of running a process as a daemon.
编辑:正如 Dagon 所指出的,这并不是将 PHP 作为“守护进程”运行的真正方式,但使用该nohup
命令可以被视为穷人将进程作为守护进程运行的方式。
回答by loki9
You can use time_sleep_until()function. It will return TRUE OR FALSE
您可以使用time_sleep_until()函数。它将返回 TRUE 或 FALSE
$interval=60; //minutes
set_time_limit( 0 );
$sleep = $interval*60-(time());
while ( 1 ){
if(time() != $sleep) {
// the looping will pause on the specific time it was set to sleep
// it will loop again once it finish sleeping.
time_sleep_until($sleep);
}
#do the routine job, trigger a php function and what not.
}
回答by Amgine
There are many ways to create a daemon in php, and have been for a very long time.
在 php 中创建守护进程的方法有很多种,而且已经有很长一段时间了。
Just running something in background isn't good. If it tries to print something and the console is closed, for example, the program dies.
只是在后台运行一些东西是不好的。例如,如果它尝试打印某些内容并且控制台已关闭,则程序将终止。
One method I have used on linux is pcntl_fork()in a php-cli script, which basically splits your script into two PIDs. Have the parent process kill itself, and have the child process fork itself again. Again have the parent process kill itself. The child process will now be completely divorced and can happily hang out in background doing whatever you want it to do.
我在 linux 上使用的一种方法是php-cli 脚本中的pcntl_fork(),它基本上将您的脚本分成两个 PID。让父进程杀死自己,让子进程再次派生自己。再次让父进程杀死自己。子进程现在将完全分离,并且可以愉快地在后台闲逛,做任何你想做的事情。
$i = 0;
do{
$pid = pcntl_fork();
if( $pid == -1 ){
die( "Could not fork, exiting.\n" );
}else if ( $pid != 0 ){
// We are the parent
die( "Level $i forking worked, exiting.\n" );
}else{
// We are the child.
++$i;
}
}while( $i < 2 );
// This is the daemon child, do your thing here.
Unfortunately, this model has no way to restart itself if it crashes, or if the server is rebooted. (This can be resolved through creativity, but...)
不幸的是,如果崩溃或服务器重新启动,此模型无法自行重新启动。(这可以通过创造力解决,但是......)
To get the robustness of respawning, try an Upstartscript (if you are on Ubuntu.) Here is a tutorial- but I have not yet tried this method.
要获得重生的稳健性,请尝试使用Upstart脚本(如果您使用的是 Ubuntu)。这是一个教程- 但我还没有尝试过这种方法。
回答by MH2K9
while(1)
means it is infinite loop. If you want to break it you should use break
by condition.
eg,.
while(1)
意味着它是无限循环。如果你想打破它,你应该break
按条件使用。例如,。
while (1){ //infinite loop
$now=time();
#do the routine job, trigger a php function and what no.
sleep($interval*60-(time()-$now));
if(condition) break; //it will break when condition is true
}