怎么判断设置函数执行时间

WBOY
Release: 2016-06-13 12:49:41
Original
1221 people have browsed it

如何判断设置函数执行时间?
function a(){
//...
return true;
}
function b(){
//...
return false;
}
现在开始调用a();

设想是如果a(),1.2秒后未返回结果,立马调用b();
有啥漂亮的处理方法?


------解决方案--------------------

不要难为自己了也不要难为PHP了

我的解决方案 
1、ajax 超时判读  缺点:时间不够准确           优点:可暂时实现此功能
2、PHP的C语言扩展 缺点:难度偏大性价比不高     优点:时间准确效率高
3、坐等大牛....
------解决方案--------------------
libevent可以实现定时器的功能,不过你的a函数应该是执行网络任务吧,如果是的话那就肯定有timeout的设置
------解决方案--------------------
php 本身就提供了此类功能
是否漂亮,你自己判断
不是什么操作都计算在 php 运行时间里的,你自己验证一下
register_shutdown_function( "time_out_callback");<br />
set_time_limit( 2 );//假设2秒就算超时<br />
//模拟超时<br />
@c();<br />
//error_reporting($es);//恢复设置<br />
<br />
function time_out_callback() {<br />
  if(connection_status() == 2) {<br />
    b();    <br />
  }<br />
}<br />
function c() {<br />
  //你的可能的超时代码<br />
  echo __FUNCTION__, ' ';<br />
  while (true) {<br />
    echo '.';<br />
    //        echo connection_status()."<br/>";//echo 0<br />
    usleep( 100000 );//给cpu透口气<br />
  }<br />
}<br />
function b() {<br />
  echo '<br>' . __FUNCTION__, ' ';<br />
  echo 'c超时 连接状态:' . connection_status();//echo 2,c超时<br />
}<br />
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!