限制php执行时间
发布时间:2020-12-13 17:40:35 所属栏目:PHP教程 来源:网络整理
导读:我做挖掘查询.有时回复很快,有时超过10秒.我的问题是我需要在5秒后停止查询,然后更新数据库.那么问题是如何在5秒后让$ip停止更新我的数据库? $host = "@$ns1 $subdomain";$ip = `/usr/bin/dig $host +short A`;// if $ip is more than 5 sec than stop the
我做挖掘查询.有时回复很快,有时超过10秒.我的问题是我需要在5秒后停止查询,然后更新数据库.那么问题是如何在5秒后让$ip停止更新我的数据库?
$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'"); 更新: 解决方法
您可以使用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(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |