<?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[[实践OK]redis的brpop简单使用,brpop的意思是在消息队列尾阻塞地取出消息，参数0表示一直阻塞下去直到有消息在msg这个list中，我们可以指定一个数字代表过期时间。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Swoole专题研究]]></category>
<pubDate>Wed, 11 Apr 2018 06:41:48 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	Q:这个brpop后面的2参数啥意思？是想模拟多个消费者吗？那个yii不也有类似消费者和swoole这个例子的差距不对差别在哪鹅？<br/>Swoole-2.1.2 进程池模块的使用：http://mp.weixin.qq.com/s?__biz=MzI0MjEwMDMzNQ==&amp;mid=2652517441&amp;idx=1&amp;sn=06909bb58cc4c0f3ade589a005ed6e84&amp;chksm=f2efe541c5986c5741aa240e9a4daaab47c542b325a0042c4fbd5094ded48221035d90003fe1&amp;mpshare=1&amp;scene=1&amp;srcid=04119CY2J0y8Az2isPm9UKyL#rd<br/><br/><br/>redis-cli <br/>127.0.0.1:6379&gt; del list1 list2<br/>(integer) 0<br/>127.0.0.1:6379&gt; rpush list1 a b c<br/>(integer) 3<br/>127.0.0.1:6379&gt; brpop list1 list2 0<br/>1) &quot;list1&quot;<br/>2) &quot;c&quot;<br/><br/><br/><br/>brpop的效果吧，首先模拟一个消息的“生产者”<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$r = new Redis();&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$r-&gt;connect(&quot;127.0.0.1&quot;, 6379);&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$r-&gt;lpush(&quot;msg&quot;,&quot;Hello ciaos&quot;);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$r-&gt;lpush(&quot;msg&quot;,&quot;Hello penjin&quot;);&nbsp;&nbsp;
?&gt;&nbsp;&nbsp;
然后模拟一个消息的消费者，而redis-server则充当中间的消息队列（我这里当然用的是redis的list类型）
[codes=php]
&lt;?php&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$r = new Redis();&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$r-&gt;connect(&quot;127.0.0.1&quot;, 6379);&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res = $r-&gt;brpop(&quot;msg&quot;,0);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var_dump($res);&nbsp;&nbsp;
?&gt;&nbsp;&nbsp;
</textarea><br/>很简单，brpop的意思是在消息队列尾阻塞地取出消息，参数0表示一直阻塞下去直到有消息在msg这个list中，我们可以指定一个数字代表过期时间。首先运行消费者，可以看到程序阻塞了，然后运行“生产者”，则消费者输出了消息如下：<br/><textarea name="code" class="php" rows="15" cols="100">
php consumer.php&nbsp;&nbsp;
</textarea><br/>array(2) &#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp;[0]=&gt;&nbsp;&nbsp;<br/>&nbsp;&nbsp;string(3) &quot;msg&quot;&nbsp;&nbsp;<br/>&nbsp;&nbsp;[1]=&gt;&nbsp;&nbsp;<br/>&nbsp;&nbsp;string(11) &quot;Hello ciaos&quot;&nbsp;&nbsp;<br/>&#125;&nbsp;&nbsp;<br/><br/>lpush操作和brpop操作也保证了队列的先进先出，如果用blpop则会看到输出的是&quot;Hello penjin&quot;(第二条消息)。<br/>其中brpop还可以指定监测多个消息队列，直到有任意一个消息队列中有待处理消息时阻塞返回，感觉还是比较简单易用的。redis的list消息队列没有优先级的概念，所有消息都只能先进先出或者后进先出，要实现优先级，可以考虑使用set数据结构。
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]redis的brpop简单使用,brpop的意思是在消息队列尾阻塞地取出消息，参数0表示一直阻塞下去直到有消息在msg这个list中，我们可以指定一个数字代表过期时间。]]></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>