2014-11-14 2 views
0

지난 며칠 동안 해결하려고 노력한 문제가 있습니다. 나는 뉴스를 크롤링하고 완벽하게 작동하는 사이트를 운영하고 있습니다. 그러나 최근 웹 서버가 설정 한 시간 제한을 초과하는 것으로 보아서 analyzer_script에 문제가 발생했습니다. 분명히 약 1 분에 max_execution 시간이 있고 내 스크립트는 그보다 오래 걸립니다. 공개 서버에서 내 웹 사이트를 호스팅하기 때문에 php.ini 스크립트에서 조정할 수 없습니다. 어떻게해야합니까? 스크립트를 다시 작성해야합니까?max_execution 제한 시간 PHP

감사합니다. 내 스크립트는 다음과 같습니다 :이 같은 스크립트의 상단에 ini_set을 사용할 수 있습니다

<?php 
    $array = array(); 
    $sub_array = array(); 
    $analyzer_ids = array(); 

$res5 = mysqli_query($con,"SELECT id,status FROM statuz ORDER BY id DESC LIMIT 1"); 
$row5 = mysqli_fetch_array($res5); 

$status = $row5['status']; 
$status_id = $row5['id']; 

if($status == 2) { 

    $res1 = mysqli_query($con,"SELECT tag, id FROM tags"); 
    while($row1 = mysqli_fetch_array($res1)) { 
     $tag = $row1['tag']; 
     $id = $row1['id']; 

     $res2 = mysqli_query($con,"SELECT sub_tag FROM sub_tags WHERE tag_id = '$id'"); 
     while($row2 = mysqli_fetch_array($res2)) { 
      $sub_tag = $row2['sub_tag']; 
      $sub_tag = strtolower($sub_tag); 
      $sub_array[] = $sub_tag; 
     } 

     $array[] = array('tag_id' => $id, 'tag' => $tag, 'sub_tag' => $sub_array); 
     $sub_array = array(); 
    } 

    mysqli_query($con,"INSERT INTO analyzer_queue (crawler_id, status) 
    (SELECT id,0 FROM crawlers)"); 

    $initial_res = mysqli_query($con,"SELECT crawler_id,id FROM analyzer_queue WHERE status = '0'"); 
    while($initial_row = mysqli_fetch_array($initial_res)) { 

     $analyzer_id = $initial_row['id']; 
     $start_crawler_id = $initial_row['crawler_id']; 
     mysqli_query($con,"UPDATE analyzer_queue SET status = '1' WHERE crawler_id = '$start_crawler_id' ORDER BY id DESC LIMIT 1"); 
     $analyzer_ids[] = $analyzer_id; 

       $res = mysqli_query($con,"SELECT cr.title, cr.content, cr.id 
       FROM crawler_results cr 
       INNER JOIN crawlers c 
       ON c.newspaper_id = cr.newspaper_id 
       WHERE c.id = '$start_crawler_id' 
       AND status = '3' 
       LIMIT 10"); 
       while($row = mysqli_fetch_array($res)) { 

        $article_id = $row['id']; 
        $title = $row['title']; 
        $content = $row['content']; 

        $content = strip_tags($content); 
        $content = strtolower($content); 
        $title = strtolower($title); 


        $count = array(); 
        foreach ($array as $tag) { 
         $regex = '/(?:\b' . preg_quote($tag['tag'], '/'); 
         foreach ($tag['sub_tag'] as $sub) { 
          $regex .= '\b)|(?:\b' . preg_quote($sub, '/'); 
         } 
         $regex .= '\b)/i'; 
         $count_content = preg_match_all($regex, $content, $count_content); 
         $count_title = preg_match_all($regex, $title, $count_title); 
         $count_total[$tag['tag']] = $count_content + $count_title; 
         $total_count = $count_total[$tag['tag']]; 

         $tag_name = $tag['tag']; 

         $res5 = mysqli_query($con,"SELECT id FROM tags WHERE tag = '$tag_name'"); 
         $row5 = mysqli_fetch_array($res5); 

         $tag_id = $row5['id']; 


         if($total_count != 0) { 
          mysqli_query($con,"INSERT INTO article_tags (article_id,tag_id,count_tags) VALUES('$article_id','$tag_id','$total_count')"); 
         } 

         echo$count_total[$tag['tag']]; 
         echo"<br /><br />"; 
        } 

        echo"<pre>"; 
        print_r($count_total); 
        echo"</pre>"; 

        mysqli_query($con,"UPDATE crawler_results SET status = '2', analyzer_id = '$analyzer_id' WHERE id = '$article_id'"); 
       } 
       mysqli_query($con,"UPDATE analyzer_queue SET status = '2' WHERE crawler_id = '$start_crawler_id' ORDER BY id DESC LIMIT 1"); 

     } 
     mysqli_query($con,"UPDATE crawler_results SET status = '4' WHERE analyzer_id NOT IN (".implode(',',$analyzer_ids).")"); 
     mysqli_query($con,"UPDATE statuz SET status = '3' WHERE id = '$status_id'"); 
     print_r($analyzer_ids); 

} else { 
    echo"Not ready yet"; 
} 




?> 
+0

당신이 매개 변수화 된 쿼리를 사용한다 mysqli''사용 ['bind_param'] (http://php.net/manual/en/mysqli- stmt.bind-param.php)를 사용하여 쿼리에 사용자 데이터를 추가하십시오. ** 심각한 [SQL injection bug] (http://bobby-tables.com/)를 만들므로 문자열 보간을 사용하여 **이를 수행하지 마십시오. – tadman

답변

0

이 시도 :

ini_set('max_execution_time', 0); 

The original Post

+0

완벽한 크라 스타 박스! 그것은 매우 빨랐다! 누구의 대답을 받아 들일 수 있습니까? oliakaoil 자세한 내용을 제공하지만, 당신은 3 분 빨리했다. –

+0

내 코드가 모두 필요한 것입니다. 나는 @oliakaoil보다 적은 점을 가지고있다. :) – KrustyBox

1

:

ini_set('max_execution_time' , 300); 

위의 호출은 5 분 스크립트의 최대 실행 시간을 설정합니다. 다음은 문서 페이지의 : 호스트가 있습니다

http://php.net/manual/en/function.ini-set.php

경우, 당신은 또한 크론 또는 다른 스케줄링 데몬을 통해이 스크립트를 실행하는 것이 좋습니다. cli 컨텍스트에서 실행되는 PHP 스크립트에는 일반적으로 실행 시간 제한이 없으며 훨씬 더 높은 PHP 스크립트가 있습니다.

+0

빠른 oliakaoil 내 질문에 대답 해 주셔서 감사합니다! 불행히도 KrustyBox는 3 분 빠르지 만 답변에 더 많은 세부 정보를 제공했습니다. 나는 무엇을해야합니까? –

+0

반갑습니다. 나는 당신의 "무엇을해야합니까?"라는 질문에 답하는 데 가장 적은 목표입니다 :-), 그래서 나는 기권 할 것입니다. – oliakaoil