<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[转载]PHP如何为函数执行设置超时]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Sun, 20 Jan 2013 09:01:02 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	如何防止一个函数执行时间过长呢？在PHP里可以用pcntl时钟信号+异常来实现。<br/>代码如下：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
declare(ticks = 1);
function a()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;sleep(10);
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;a finishi&#92;n&quot;;
&#125;
function b()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;Stop&#92;n&quot;;
&#125;
function c()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;usleep(100000);
&#125;
 
function sig()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;throw new Exception;
&#125;
 
try
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;pcntl_alarm(1);
&nbsp;&nbsp;&nbsp;&nbsp;pcntl_signal(SIGALRM, &quot;sig&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;a();
&nbsp;&nbsp;&nbsp;&nbsp;pcntl_alarm(0);
&#125;
catch(Exception $e)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;timeout&#92;n&quot;;
&#125;
 
b();
a();
b();
</textarea><br/>root@192.168.137.158:~/php/swoole_study_codes# php timeout.php<br/>PHP Exception:&nbsp;&nbsp; in /root/php/swoole_study_codes/timeout.php on line 19<br/>PHP Stack trace:<br/>PHP&nbsp;&nbsp; 1. &#123;main&#125;() /root/php/swoole_study_codes/timeout.php:0<br/>PHP&nbsp;&nbsp; 2. a() /root/php/swoole_study_codes/timeout.php:26<br/>PHP&nbsp;&nbsp; 3. sig() /root/php/swoole_study_codes/timeout.php:5<br/>timeout<br/>Stop<br/>a finishi<br/>Stop<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; 原理是在函数执行前先设定一个时钟信号，如果函数的执行超过规定时间，信号会被触发，信号处理函数会抛出一个异常，被外层代码捕获。这样就跳出了原来函数的执行，接着执行下面的代码。如果函数在规定的时间内，时钟信号不会触发，在函数结束后清除时钟信号，不会有异常抛出。<br/>来源自rango兄弟的博客：http://swoole.sinaapp.com/archives/64
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [转载]PHP如何为函数执行设置超时]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>