php - 限制php执行时间

标签 php

我进行挖掘查询。有时回复很快,有时超过 10 秒。我的问题是我需要在 5 秒后停止查询,然后更新数据库。所以这就是我如何让 $ip 在 5 秒后停止更新我的数据库的问题?

$host = "@$ns1 $subdomain";
$ip = `/usr/bin/dig $host +short A`;

// if $ip is more than 5 sec than stop the query. How to do this?

mysql_query("UPDATE dns SET query_ns = '1' WHERE zone ='123'");

更新: 对于任何混淆,我深表歉意。 query 的意思是使用 dig 进行 ns 查找。再次抱歉。

最佳答案

您可以使用 proc_open 打开 dig 命令的管道,stream_select 它并等待 5 秒,之后读取并关闭 proc。

或多或少是这样的:

function getip()
{
$ip = null;

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")  // stderr
);

$process = proc_open("/usr/bin/dig $host +short A", $descriptorspec, $pipes);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // 2 => readable handle


    $ip = fgetsPending($pipes[1]);

    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    proc_close($process);
}

return $ip;
}

function fgetsPending(&$in,$tv_sec=5) 
{
    if ( stream_select($read = array($in),$write=NULL,$except=NULL,$tv_sec) ) return fgets($in);
    else return FALSE;   
}

echo getip();

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

相关文章:

php - 以编程方式从 WordPress 中删除图像

php - 使用 php 和 mysql 从多个数组表单输入插入数据

php - 正则表达式从 HTML 中删除 id 属性

php - ajax xhr lengthComputable用php文件返回false

PHP/MYSQL session - 打印当前用户名

javascript - PHP - 从文本区域中删除换行符

一旦我添加 'echo' 并从表中获取一些内容,PHP (MySQL) 脚本就不会读取任何其他内容

php - 将 BASH 关联数组传递给 PHP 脚本

php - 为什么我不能在函数的返回值上使用数组索引?

php - fwrite() 和 UTF8