我做挖掘查询。有时回复速度很快,有时超过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查找。再一次抱歉。
发布于 2012-02-14 17:16:39
您可以使用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();发布于 2012-02-14 17:10:14
查看mysql_unbuffered_query函数的文档:http://www.php.net/manual/en/function.mysql-unbuffered-query.php
这样的事情应该做你想做的事:
<?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;
}
}
}
?>发布于 2012-02-14 17:14:41
只是在黑暗中打了一枪,但我会尝试这样的方法:
$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);https://stackoverflow.com/questions/9281060
复制相似问题