首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >限制php执行时间

限制php执行时间
EN

Stack Overflow用户
提问于 2012-02-14 17:05:37
回答 7查看 738关注 0票数 4

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

代码语言:javascript
复制
$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查找。再一次抱歉。

EN

回答 7

Stack Overflow用户

发布于 2012-02-14 17:16:39

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

差不多是这样的:

代码语言:javascript
复制
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();
票数 3
EN

Stack Overflow用户

发布于 2012-02-14 17:10:14

查看mysql_unbuffered_query函数的文档:http://www.php.net/manual/en/function.mysql-unbuffered-query.php

这样的事情应该做你想做的事:

代码语言:javascript
复制
<?php
// a db link for queries
$lh  = mysql_connect( 'server', 'uname', 'pword' );
// and a controller link
$clh = mysql_connect( 'server', 'uname', 'pword', true );

if ( mysql_select_db ( 'big_database', $lh ) )
{
  $began  =  time();
  $tout   = 60 * 5; // five minute limit
  $qry    = "SELECT * FROM my_bigass_table";
  $rh     = mysql_unbuffered_query( $qry, $lh );
  $thread = mysql_thread_id ( $lh );
  while ( $res = mysql_fetch_row( $rh ) )
  {
    /* do what you need to do
     * ...
     * ...
     */
    if ( ( time() - $began ) > $tout )
    {
      // this is taking too long
      mysql_query( "KILL $thread", $clh );
      break;
    }
  }
}
?>
票数 1
EN

Stack Overflow用户

发布于 2012-02-14 17:14:41

只是在黑暗中打了一枪,但我会尝试这样的方法:

代码语言:javascript
复制
$default_exe_time = ini_get('max_execution_time');
try {
    ini_set('max_execution_time', 5);
    $host = "@$ns1 $subdomain";
    $ip = `/usr/bin/dig $host +short A`;
    if (!mysql_query("UPDATE dns SET query_ns = '1' WHERE zone ='123'")){
        throw new Exception('');
    }
} catch Exception ($e) {
   // update db
}
ini_set('max_execution_time', $default_exe_time);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9281060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档