<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://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://jackxiang.com/post//</link>
<title><![CDATA[如何让exec（）自己运行，php页面继续运行，让PHP不再阻塞。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Wed, 10 Dec 2014 06:50:10 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：用Nginx上传插件实现大文件分片上传后，处理子程序的程序需要每次对分片进行合并，但又不想耗费PHP太多内存，只好让shell来干这事儿，同时呢，又不想让PHP用exec合并文件时出现等待，怎么办？<br/><br/>&nbsp;&nbsp; 在php中，如果出现exec（）；那么php是先运行完exec中的外部命令，然后继续执行接下来的php语句的。有什么办法让exec自己在那运行，php语句不等待，自己运行下去，让浏览器不用等待。谢谢。<br/><br/>Note: <br/>如何程序使用此函数启动，为了能保持在后台运行，此程序必须将输出重定向到文件或其它输出流。 否则会导致 PHP 挂起，直至程序执行结束。<br/>exec(&quot;命令 &gt; null&quot;);<br/><br/>来自：http://zhidao.baidu.com/link?url=sqz2GSYfd3fJWE5F7F_2AR15zpj6VNe-9jc1pMNmmj-3MB5KHOnbzOB_9Dls43TqL1zH0PZmdZuN1AXSc_Xj-_<br/><br/>——————————————————————————————————————————————————————————————<br/>请问在php程序里如何非阻塞地执行另一个.php脚本？<br/><br/>我们知道在命令行里可以用php XX.php调用一个php脚本，<br/>但是在php程序里怎样调用另一个.php脚本呢？<br/>要求有两个：<br/>(1) 非阻塞式的。即主程序执行调用其他脚本的语句后，继续执行下面的语句而不阻塞。<br/>(2) 能否把一些主程序的对象作为参数传给被调用的脚本？可以以变通的方式。<br/><br/>exec(&quot;php XX.php&quot;)函数貌似不行，因为exec()是阻塞的。<br/>请大家想想办法，如何满足以上这两个解决问题？谢谢！<br/><br/><br/>刚看了看Process Control Functions的说明(英文的没看太明白).不过看到了pcntl_fork()复制进程.<br/>这个和Perl的fork()差不多能实现.不过Process Control Functions不能在windows下使用啊,只能在unix或linux下用.比较不爽啊!!!!!<br/>示範 1.pcntl_fork() example<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$pid = pcntl_fork();
if ($pid == -1) &#123;
&nbsp;&nbsp;&nbsp;&nbsp; die(&quot;could not fork&quot;);
&#125; else if ($pid) &#123;
&nbsp;&nbsp;&nbsp;&nbsp; // we are the parent
&#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp; // we are the child
&#125;
?&gt; 
</textarea><br/>主进程执行当前程序,让子进程去调用另一个php脚本吧,这不就不阻塞了吗!!!<br/><br/><br/>来自：http://bbs.phpchina.com/thread-23687-1-1.html<br/><br/><br/>当PHP作为后端处理需要完成一些长时间处理，为了快速响应页面请求，不作结果返回判断的情况下，可以有如下措施：<br/><br/>一、若你使用的是FastCGI模式，使用fastcgi_finish_request()能马上结束会话，但PHP线程继续在跑。<br/><textarea name="code" class="php" rows="15" cols="100">
echo &quot;program start.&quot;;
 
file_put_contents(&#039;log.txt&#039;,&#039;start-time:&#039;.date(&#039;Y-m-d H:i:s&#039;), FILE_APPEND);
fastcgi_finish_request();
sleep(1);
echo &#039;debug...&#039;;
file_put_contents(&#039;log.txt&#039;, &#039;start-proceed:&#039;.date(&#039;Y-m-d H:i:s&#039;), FILE_APPEND);
 
sleep(10);
file_put_contents(&#039;log.txt&#039;, &#039;end-time:&#039;.date(&#039;Y-m-d H:i:s&#039;), FILE_APPEND);
</textarea><br/>这个例子输出结果可看到输出program start.后会话就返回了，所以debug那个输出浏览器是接收不到的，而log.txt文件能完整接收到三个完成时间。<br/><br/>二、使用fsockopen的非阻塞模式请求另外的网址<br/><textarea name="code" class="php" rows="15" cols="100">
$fp = fsockopen(&quot;www.example.com&quot;, 80, $errno, $errstr, 30);
if (!$fp) die(&#039;error fsockopen&#039;);
stream_set_blocking($fp,0);
$http = &quot;GET /save.php&nbsp;&nbsp;/ HTTP/1.1&#92;r&#92;n&quot;;&nbsp;&nbsp;&nbsp;&nbsp;
$http .= &quot;Host: www.example.com&#92;r&#92;n&quot;;&nbsp;&nbsp;&nbsp;&nbsp;
$http .= &quot;Connection: Close&#92;r&#92;n&#92;r&#92;n&quot;;
fwrite($fp,$http);
fclose($fp);
</textarea><br/>三、使用Gearman<br/>Gearman是一个具有php扩展的分布式异步处理框架，能处理大批量异步任务。<br/>摘自：http://blog.4wer.com/php-nonblock<br/>PHP中实现非阻塞模式：<br/>http://blog.csdn.net/linvo/article/details/5466046
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 如何让exec（）自己运行，php页面继续运行，让PHP不再阻塞。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>