<?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的任务队列，老外用PHP扩展的Libevent写的一个基于ZEROMQ队列]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 30 Apr 2009 06:31:28 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp; http://toys.lerdorf.com/archives/57-ZeroMQ-+-libevent-in-PHP.html<br/>&nbsp;&nbsp; 写了一个简单的队列任务处理。多进程任务，异步任务可能会用到这个(主要是命令行应用)<br/>比如，任务的某个一个环节速度十分不稳定，可能执行几秒，也可能执行几分钟，<br/>我就可以把那个环节包括前面的部分扔进队列，多跑几个进程，同时往队列里面写。<br/>然后后面比较快的环节只跑一个处理任务就OK了。让整体速度达到更好的效果。<br/><br/>write.php: 将任务写入队列<br/><br/><br/><div class="code">&lt;?php<br/>/*<br/>产生队列<br/>*/<br/>//用微秒生成队列文件名。因为会有多个队列，所以加了一个identifier来区分各个队列<br/>function mt($identifier=&#039;default&#039;)<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return sprintf(&quot;%.6f.%s&quot;,strtok(microtime(),&#039; &#039;)+strtok(&#039;&#039;),$identifier);<br/>&#125;<br/><br/>while(1) //实际中尽量不要while(1) 非要while(1)记得任务完成要break<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(count(glob(&#039;./queue/*.identifier&#039;))&gt;=10) //队列最大长度，不限制的话硬盘可能会受不了哦。<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);//记住这里要sleep,否则队列满了cpu占用很高<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$url = &#039;www.&#039;.time().&#039;.com&#039;; //随便举个例子，我用时间戳生成了一个网址<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;$url&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$fp = fopen(&#039;./queue/&#039;.mt(&#039;identifier&#039;),&#039;w&#039;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fwrite($fp,$url);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose($fp);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);//这里不需要sleep，我sleep是因为我的任务太简单。<br/>&#125;<br/>?&gt;</div><br/><br/>read.php:<br/><br/><br/><div class="code">&lt;?php<br/>/*<br/>处理队列<br/>*/<br/><br/>while(1) //实际程序最好不要while(1)如果while(1)，记得处理完任务要break<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($queue = glob(&#039;./queue/*.identifier&#039;))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$q = array_shift($queue);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$url = file_get_contents($q);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $url.&quot;&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unlink($q);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);//这里要不要sleep或sleep多久自己凭感觉来。<br/>&#125;<br/>?&gt;</div><br/><br/><br/>相关的资料：双向队列<br/><br/>baidu和google上没有查到PHP双向队列的资料，搜索到java的双向队列定义如下：双向队列（双端队列）就像是一个队列，但是你可以在任何一端添加或移除元素。<br/>而双端队列是一种数据结构，定义如下：<br/>A deque is a data structure consisting of a list of items, on which the following operations are possible.<br/>* push(D,X) -- insert item X on the rear end of deque D.<br/>* pop(D) -- remove the front item from the deque D and return it.<br/>* inject(D,X) -- insert item X on the front end of deque D.<br/>* eject(D) -- remove the rear item from the deque D and return it.<br/>Write routines to support the deque that take O(1) time per operation.<br/><br/>翻译：双端队列（deque）是由一些项的表组成的数据结构，对该数据结构可以进行下列操作：<br/>push(D,X) 将项X 插入到双端队列D的前端<br/>pop(D) 从双端队列D中删除前端项并将其返回<br/>inject(D,X) 将项X插入到双端队列D的尾端<br/>eject(D) 从双端队列D中删除尾端项并将其返回<br/>编写支持双端队伍的例程，每种操作均花费O（1）时间<br/><br/>百度百科：（deque，全名double-ended queue）是一种具有队列和栈的性质的数据结构。双端队列中的元素可以从两端弹出，其限定插入和删除操作在表的两端进行。<br/><br/>转贴一个使用Php数组函数实现该功能的代码：<br/><br/><div class="code">&lt;?php<br/>//Input limit double-ende queue<br/>class DoubleEndedQueue1 &#123;<br/>var $queue = array();<br/>function add($var)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return array_push($this-&gt;queue, $var);<br/>&#125;<br/>function frontRemove()&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return array_shift($this-&gt;queue);<br/>&#125;<br/>function rearRemove()&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return array_pop($this-&gt;queue);<br/>&#125;<br/>&#125;<br/><br/>//Output limit double-ende queue<br/>class DoubleEndedQueue2 &#123;<br/>var $queue = array();<br/>function remove()&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return array_pop($this-&gt;queue);<br/>&#125;<br/>function frontAdd($var)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return array_unshift($this-&gt;queue, $var);<br/>&#125;<br/>function rearAdd($var)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return array_push($this-&gt;queue, $var);<br/>&#125;<br/>&#125;<br/><br/>//Test code<br/>$q = new DoubleEndedQueue1;<br/>$q-&gt;add(&#039;aaa&#039;);<br/>$q-&gt;add(&#039;bbb&#039;);<br/>$q-&gt;add(&#039;ccc&#039;);<br/>$q-&gt;add(&#039;ddd&#039;);<br/><br/>echo $q-&gt;frontRemove();<br/>echo &quot;&lt;br&gt;&quot;;<br/>echo $q-&gt;rearRemove();<br/>echo &quot;&lt;br&gt;&quot;;<br/>print_r($q-&gt;queue);<br/>?&gt;</div><br/><br/>array_push -- 将一个或多个单元压入数组的末尾（入栈）<br/>array_unshift -- 在数组开头插入一个或多个单元<br/>array_pop -- 将数组最后一个单元弹出（出栈）<br/>array_shift -- 将数组开头的单元移出数组<br/><br/>// 来自 PHP5 in Practice&nbsp;&nbsp;(U.S.)Elliott III &amp; Jonathan D.Eisenhamer<br/><br/><br/><div class="code">&lt;?php<br/>// A library to implement queues in PHP via arrays<br/>// The Initialize function creates a new queue:<br/>function &amp;queue_initialize() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// In this case, just return a new array<br/>&nbsp;&nbsp;&nbsp;&nbsp;$new = array();<br/>&nbsp;&nbsp;&nbsp;&nbsp;return $new;<br/>&#125;<br/>// The destroy function will get rid of a queue<br/>function queue_destroy(&amp;$queue) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Since PHP is nice to us, we can just use unset<br/>&nbsp;&nbsp;&nbsp;&nbsp;unset($queue);<br/>&#125;<br/>// The enqueue operation adds a new value unto the back of the queue<br/>function queue_enqueue(&amp;$queue, $value) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// We are just adding a value to the end of the array, so can use the <br/>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&#91;&#93; PHP Shortcut for this.&nbsp;&nbsp;It&#039;s faster than using array_push<br/>&nbsp;&nbsp;&nbsp;&nbsp;$queue&#91;&#93; = $value;<br/>&#125;<br/>// Dequeue removes the front of the queue and returns it to you<br/>function queue_dequeue(&amp;$queue) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Just use array unshift<br/>&nbsp;&nbsp;&nbsp;&nbsp;return array_shift($queue);<br/>&#125;<br/>// Peek returns a copy of the front of the queue, leaving it in place<br/>function queue_peek(&amp;$queue) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Return a copy of the value found in front of queue <br/>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;(at the beginning of the array)<br/>&nbsp;&nbsp;&nbsp;&nbsp;return $queue&#91;0&#93;;<br/>&#125;<br/>// Size returns the number of elements in the queue<br/>function queue_size(&amp;$queue) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Just using count will give the proper number:<br/>&nbsp;&nbsp;&nbsp;&nbsp;return count($queue);<br/>&#125;<br/>// Rotate takes the item on the front and sends it to the back of the queue.<br/>function queue_rotate(&amp;$queue) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Remove the first item and insert it at the rear.<br/>&nbsp;&nbsp;&nbsp;&nbsp;$queue&#91;&#93; = array_shift($queue);<br/>&#125;<br/>// Let&#039;s use these to create a small queue of data and manipulate it.<br/>// Start by adding a few words to it:<br/>$myqueue =&amp; queue_initialize();<br/>queue_enqueue($myqueue, &#039;Opal&#039;);<br/>queue_enqueue($myqueue, &#039;Dolphin&#039;);<br/>queue_enqueue($myqueue, &#039;Pelican&#039;);<br/>// The queue is: Opal Dolphin Pelican<br/>// Check the size, it should be 3<br/>echo &#039;&lt;p&gt;Queue size is: &#039;, queue_size($myqueue), &#039;&lt;/p&gt;&#039;;<br/>// Peek at the front of the queue, it should be: Opal<br/>echo &#039;&lt;p&gt;Front of the queue is: &#039;, queue_peek($myqueue), &#039;&lt;/p&gt;&#039;;<br/>// Now rotate the queue, giving us: Dolphin Pelican Opal<br/>queue_rotate($myqueue);<br/>// Remove the front element, returning: Dolphin<br/>echo &#039;&lt;p&gt;Removed the element at the front of the queue: &#039;, <br/>&nbsp;&nbsp;&nbsp;&nbsp;queue_dequeue($myqueue), &#039;&lt;/p&gt;&#039;;<br/>// Now destroy it, we are done.<br/>queue_destroy($myqueue);<br/>?&gt;</div><br/><br/>
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [原创]简单的PHP的任务队列，老外用PHP扩展的Libevent写的一个基于ZEROMQ队列]]></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>