Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php永无止境循环_Php_Loops - Fatal编程技术网

php永无止境循环

php永无止境循环,php,loops,Php,Loops,我需要一个在php中独立执行的函数,而不需要crone的帮助。我已经想出了下面的代码,对我来说很好,但由于它是一个永无止境的循环,它会导致任何问题,我的服务器或脚本,如果是这样,你能给我一些建议或替代方案,请。谢谢 $interval=60; //minutes set_time_limit(0); while (1){ $now=time(); #do the routine job, trigger a php function and what not. slee

我需要一个在php中独立执行的函数,而不需要crone的帮助。我已经想出了下面的代码,对我来说很好,但由于它是一个永无止境的循环,它会导致任何问题,我的服务器或脚本,如果是这样,你能给我一些建议或替代方案,请。谢谢

$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));
}
而(1)
表示它是无限循环。如果要断开,应按条件使用
break
。 例如


我们在实时系统环境中使用了无限循环,基本上是等待收到的SMS,然后对其进行处理。我们发现这样做会使服务器资源随着时间的推移而变得密集,并且必须重新启动服务器以释放内存

我们遇到的另一个问题是,当您在浏览器中执行带有无限循环的脚本时,即使您点击停止按钮,它也会继续运行,除非您重新启动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.
    }
解决方案是在命令行上以deamon的形式运行PHP脚本。以下是方法:

nohup php myscript.php&

&
将您的流程置于后台

我们不仅发现此方法占用内存较少,而且还可以通过运行以下命令在不重新启动apache的情况下终止它:

kill processid

编辑:正如Dagon指出的,这并不是将PHP作为“守护进程”运行的真正方式,但是使用
nohup
命令可以被认为是穷人将进程作为守护进程运行的方式。

您可以使用函数。它将返回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.
   }

有很多方法可以在php中创建守护进程,并且已经存在了很长一段时间

只是在后台运行一些东西是不好的。例如,如果它试图打印某些内容,并且控制台关闭,则程序将死亡

我在linux上使用的一种方法是在PHPCLI脚本中,它基本上将脚本拆分为两个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.
$i=0;
做{
$pid=pcntl_fork();
如果($pid==-1){
模具(“无法分叉,正在退出。\n”);
}否则如果($pid!=0){
//我们是父母
模具(“i级分叉工作,退出。\n”);
}否则{
//我们是孩子。
++$i;
}
}而($i<2);
//这是守护进程的孩子,在这里做你的事情。
不幸的是,如果此模型崩溃或服务器重新启动,则无法自行重新启动。(这可以通过创造性来解决,但是……)


为了获得重生的健壮性,可以尝试一个脚本(如果你在Ubuntu上的话)-但我还没有尝试过这种方法。

实际上我不想让它崩溃,我只想知道后果是什么。你想要一个php守护进程,我个人不会将php用于守护进程。总是会有内存泄漏,你会遇到,将不得不重新启动它每个月左右。我建议您改用另一种编译语言。作为您对php的新手,也许您应该解释一下为什么需要这种语言,可能有更好的方法idea@Dagon我正在构建一个web应用程序,这基本上是每15分钟循环一次订单,并将其分配给一个免费的delevary。从命令行调用php脚本并不会使其成为Daemont。这种过程是Apache和php不擅长的,但node.js等新技术所做的-依我看,这些任务不应该用php完成,它不可避免地会回来咬你。@Dagon什么会使它成为一个守护进程?刚刚发现,它比我更全面地回答了这个问题。
$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.