我正在尝试将基于BookSleeve的超级简单排队示例放在一起.
以下是我目前为队列消费者方面所做的事情:
using System;
using System.Text;
using BookSleeve;
static class Program
{
static void Main()
{
using (var redis = new RedisConnection("127.0.0.1"))
{
redis.Open();
while (true)
{
// BRPOP the queue
var t = redis.Lists.BlockingRemoveFirst(0, new[] { "test" }, 0);
t.Wait();
var val = Encoding.UTF8.GetString(t.Result.Item2);
Console.WriteLine("Got: {0}", val);
}
}
}
}
我在LINQPad作为制作人运行以下内容:
using(var redis = new RedisConnection("localhost"))
{
redis.Open();
foreach(var i in Enumerable.Range(0, 10))
{
// LPUSH
redis.Lists.AddFirst(0, "test", "foo" + i)
// Call wait to keep things in order
.Wait();
}
Thread.Sleep(500); // Let Redis do it's thing
Console.WriteLine("queue length: " + redis.Lists.GetLength(0, "test").Result);
}
我得到了一些非常奇怪的结果:
第一轮:
Got: foo2
Got: foo5
Got: foo7
Got: foo9
第二轮:
Got: foo1
Got: foo4
Got: foo7
第三轮:
Got: foo0
Got: foo3
Got: foo6
Got: foo8
此外,每次运行后,LINQPad输出:队列长度:0,并使用实际的redis客户端运行LLEN测试返回(整数)0.
我不禁觉得我在这里遗漏了一些东西,很可能与异步的东西有关,但我只是看不到它.
我的redis版本是2.8.3,BookSleeve版本是1.3.41.
这里的实际问题是:为什么BookSleeve只返回发送到redis列表的消息的子集?
最佳答案 我根本无法弄错.你绝对确定你只有1个消费者吗?请注意,订单不可预测的原因是因为您将其用作堆栈而不是队列 – 要获得可靠的订单,您应该添加到开头并从末尾删除,或者添加到结尾并从头开始删除.如果你从头开始添加和删除它是随机的顺序.
但是,它对我来说很好.控制台输出:
Got: foo0
Got: foo1
Got: foo3
Got: foo5
Got: foo6
Got: foo8
Got: foo9
Got: foo7
Got: foo4
Got: foo2
queue length: 0
我建议在监控模式下使用redis-cli – 对我来说,我得到以下输出:
redis 127.0.0.1:6379> monitor
OK
1389454168.068869 [0 127.0.0.1:4957] "INFO"
1389454168.068869 [0 127.0.0.1:4957] "CONFIG" "GET" "timeout"
1389454168.068869 [0 127.0.0.1:4957] "DEL" "test"
1389454168.129869 [0 127.0.0.1:4958] "INFO"
1389454168.129869 [0 127.0.0.1:4958] "CONFIG" "GET" "timeout"
1389454168.136869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.139872 [0 127.0.0.1:4959] "INFO"
1389454168.139872 [0 127.0.0.1:4959] "CONFIG" "GET" "timeout"
1389454168.139872 [0 127.0.0.1:4959] "LPUSH" "test" "foo0"
1389454168.142869 [0 127.0.0.1:4959] "LPUSH" "test" "foo1"
1389454168.142869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.142869 [0 127.0.0.1:4959] "LPUSH" "test" "foo2"
1389454168.143871 [0 127.0.0.1:4959] "LPUSH" "test" "foo3"
1389454168.143871 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.143871 [0 127.0.0.1:4959] "LPUSH" "test" "foo4"
1389454168.143871 [0 127.0.0.1:4959] "LPUSH" "test" "foo5"
1389454168.144870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.144870 [0 127.0.0.1:4959] "LPUSH" "test" "foo6"
1389454168.144870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.144870 [0 127.0.0.1:4959] "LPUSH" "test" "foo7"
1389454168.145869 [0 127.0.0.1:4959] "LPUSH" "test" "foo8"
1389454168.145869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.145869 [0 127.0.0.1:4959] "LPUSH" "test" "foo9"
1389454168.145869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.146869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.146869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.147870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.147870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.648594 [0 127.0.0.1:4959] "LLEN" "test"
这里连接4957是一个只删除密钥(以确保已知状态); 4958是消费者,4959是生产者.
作为参考,我在上面的测试中使用了2.6.12 Windows build(因为我在我的Windows笔记本电脑上,并且没有时间设置linux VM) – 但是:我希望在官方的linux版本中也是如此.
所以:你确定(通过redis-cli)你只有2个连接吗?
如果我改为AddLast:
Got: foo0
Got: foo1
Got: foo2
Got: foo3
Got: foo4
Got: foo5
Got: foo6
Got: foo7
Got: foo8
Got: foo9
queue length: 0