出现500错误-在PHP中执行长循环


A 500 error has occurred - Executing long loop within PHP

在用PHP执行长循环时发生了一个500错误。

我目前正在开发一个调度系统来调度医院病房的护士,我正在使用遗传算法来实现这一点。

所以我把每个护士随机分配到一个班次。然后计算出他们适合轮班的程度。然后,我会取消任何不符合我体能水平的分配。

然后我随机分配一个新的时间表。评估是否适合取消任何不符合我体能水平的分配。

合并两个时间表,保持健身分配

我循环生成随机时间表,访问其适用性并合并时间表

这在循环通过30-100次时工作良好

一旦我超过100分,它有时会失败——出现了500分的错误当完成脚本需要超过2:30分钟时,就会发生这种情况

所以我假设在某个时候我的服务器会因为花费太长时间而超时?

我添加了<?php set_time_limit(3600);

这是在我的文件的顶部,而不是在构造函数或类中。它在正确的地方吗
它仍然在2分半钟时超时,

这是我的代码,循环是200循环仍然需要折射我的代码,所以不要太挑剔

<?php
set_time_limit(3600);
 * Description of scheduler
 *
 * @author Dela
 */
include ("randomTtAllocation.php");
include ("fittness.php");
class scheduler {
private $randomTimetable;
private $timetable;
private $weight;
private $newTimetable ;
private $newWeight;
    function __construct($labs,$students) {
        echo "helloworld";
        // create random timetable and print it 
         $r = new randomTtAllocation();
         $this->randomTimetable = $r->__randomAllocation($labs, $students); 
         //echo "<br>" . " ............initial Time Table............." . "<br>" ;
         echo "<br>";
          echo "<br>";
           echo "<br>";

         $this->__printTt($this->randomTimetable, $this->randomTimetable);
         // work out fittness for the timetable, return fit results and print 
         $fit = new fittness( $this->randomTimetable, $labs, $students , $r->__getNumOfSessions());

         $this->newWeight = $fit->__getnewWeight();
         $this->newTimetable = $fit->__getnewTimetable();;

         //echo "<br>" . " ............newTimetable.........newWeight...." . "<br>" ;
         //$this->__printTt($this->newTimetable, $this->newWeight );
         // sort 
         $this->__sortTtByWeight();
         $this->timetable = $this->newTimetable;
         $this->weight =  $this->newWeight;

      for ($i = 0; $i < 200; $i++) {
         // create second time table 
         $this->randomTimetable = $r->__randomAllocation($labs, $students); 
        // echo "<br>" . " ............initial Time Table............." . "<br>" ;
        // $this->__printTt($this->randomTimetable, $this->randomTimetable);
         // work out fittness for the timetable, return fit results and print 
         $fit = new fittness( $this->randomTimetable, $labs, $students , $r->__getNumOfSessions());
         $this->newWeight = $fit->__getnewWeight();
         $this->newTimetable = $fit->__getnewTimetable();
         //echo "<br>" . " ............tempTimetable.........tempWeight...." . "<br>" ;
         //$this->__printTt($this->newTimetable, $this->timetable );
         $this->__sortTtByWeight();
         // merge timetables 
         echo "<br>" . " ......old...........new." . "<br>" ;
         $this->__printTt($this->timetable,$this->newTimetable );
         $this->__mergeTimetables($this->newTimetable, $this->newWeight );      
         //echo "<br>" . " ........... mergedTimetable.........newWeight...." . "<br>" ;
        //$this->__printTt($this->timetable,$this->weight );

         // fittness of new timetable
         $fit = new fittness( $this->timetable, $labs, $students , $r->__getNumOfSessions());
          echo "<br>" . " ......merged.......kulled" . "<br>" ;
         $this->__printTt($this->timetable,$fit->__getnewTimetable() );
         $this->weight = $fit->__getnewWeight();
         $this->timetable = $fit->__getnewTimetable();
           $c[$i] = $this->__countSlotsAllocated();
         echo "<br> ". $i;
     }
  print_r($c);
    }
     // sorts the re allocated time table by weight
     function __sortTtByWeight() {
                // for each slot
            foreach($this->newTimetable as $l => $i_value) {
            $size = 0;
            // see how many sessions they are taking
            while ($this->newTimetable[$l][$size] != "null") {
                if ($this->newTimetable[$l][$size] == "0") {
                    break;
                }
                $size++;
            }
            for ($i = 1; $i < $size; $i++) {
                $key = $this->newWeight[$l][$i];
                $key1 = $this->newTimetable[$l][$i];
                $k = $i - 1;
                while ($k >= 0 && $this->newWeight[$l][$k] < $key) {
                    $this->newWeight[$l][$k + 1] = $this->newWeight[$l][$k];
                    $this->newTimetable[$l][$k + 1] = $this->newTimetable[$l][$k];
                    $k--;
                }
                $this->newTimetable[$l][$k + 1] = $key1;
                $this->newWeight[$l][$k + 1] = $key;
            }
        }    
     }  
       function __countSlotsAllocated() {
           $count = 0;
           foreach($this->timetable as $i => $x_value) {
               $j = 0;
             while (($this->timetable[$i][$j] != "null") && ($this->timetable[$i][$j] != "0")){
                $count++;
                $j++;
             }
           }
           echo "count " . $count;
           return $count;
       }
     function __mergeTimetables($tempTimeTable,$tempWeight) {
        // for each session
         foreach($this->newTimetable as $i => $i_value) {
            $j = 0;
            $k = 0;
            // while there are still students
            while (($tempTimeTable[$i][$j] != "null") && ($tempTimeTable[$i][$j] != "0")) {
                            //echo $tempTimeTable[$i][$j];
                // System.out.println(timeTable[i][k]);
                // see if there is a free gap
                while ($this->timetable[$i][$k] != "null") {
                    // if student is already taking that session
                    if ($tempTimeTable[$i][$j] == $this->timetable[$i][$k]) {
                        break;
                    }
                    if ($this->timetable[$i][$k] == "0") {
                        $this->timetable[$i][$k] = $tempTimeTable[$i][$j];
                        $this->weight[$i][$k] = $tempWeight[$i][$j];
                        break;
                    }
                    $k++;
                }
                if ($this->timetable[$i][$k] == "null") {
                    if ($tempWeight[$i][$j] < $this->weight[$i][0]) {
                        $this->timetable[$i][0] = $tempTimeTable[$i][$j];
                        $this->weight[$i][0] = $tempWeight[$i][$j];
                    }
                }
                $j++;
            }
        }
     }
      function __returnTimetable() {
          return $this->timetable;
      }

      function __printTt($timetable, $weight) {
           foreach($timetable as $x => $x_value) {
             for ($i = 0; $i < 5; $i++) {
                echo $timetable[$x][$i] . " ";
             }
             echo " . . . . .";
             for ($i = 0; $i < 5; $i++) {
                echo $weight[$x][$i] . " ";
             }
             echo "<br>";
             }
         }
}

通常不应该在http请求中完成所有这些工作。相反,你应该启动一个后台任务,并让网页显示工作进度。

php配置对每个请求都有一个时间限制,您可以对其进行调整,但用户预计不会花这么长时间。