gpt4 book ai didi

php - php max_execution 时间限制

转载 作者:行者123 更新时间:2023-11-29 12:34:54 24 4
gpt4 key购买 nike

过去几天我一直在尝试解决一个问题。我有一个可以抓取新闻的网站,效果非常好。然而最近,我的分析器脚本遇到了问题,因为它似乎超出了我的网络主机设置的时间限制。显然,最大执行时间约为 1 分钟,而我的脚本花费的时间比这长得多。而且我无法在 php.ini 脚本中调整它,因为我将我的网站托管在公共(public)服务器上。我能做些什么?我需要重写我的脚本吗?

感谢您的帮助!我的脚本如下:

<?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";
}




?>

最佳答案

您可以在脚本顶部使用 ini_set,如下所示:

ini_set('max_execution_time' , 300 );

上述调用会将脚本的最大执行时间设置为五分钟。这是文档页面:

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

如果您的主机允许,您还可以考虑通过 cron 或其他一些调度守护进程运行此脚本。在 cli 上下文中运行的 PHP 脚本通常没有执行时间限制,或者执行时间更长。

关于php - php max_execution 时间限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26938773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com