gpt4 book ai didi

c# - BookSleeve BlockingRemoveLeft 只返回一些插入列表的项目

转载 作者:IT王子 更新时间:2023-10-29 05:59:54 25 4
gpt4 key购买 nike

我正在尝试根据 BookSleeve 组合一个 super 简单的排队示例。

到目前为止,这是我在队列消费者方面所拥有的:

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 输出:queue length: 0,使用实际的 redis 客户端运行 LLEN test 返回 (integer) 0.

我忍不住觉得我在这里遗漏了一些东西,很可能与异步的东西有关,但我就是看不到它。

我的redis版本是2.8.3,BookSleeve版本是1.3.41。

这里的实际问题是:为什么 BookSleeve 只返回发送到 redis 列表的消息的一个子集?

最佳答案

我根本无法让它出错。您绝对确定您只有 1 个消费者吗?请注意,order 不可预测的原因是因为您将它用作堆栈而不是队列 - 要获得可靠的顺序,您应该添加到开头并从结尾删除,或者添加到结束并从头开始删除。如果您从头开始添加和删除,它是随机顺序。

但是,它对我来说效果很好。控制台输出:

Got: foo0
Got: foo1
Got: foo3
Got: foo5
Got: foo6
Got: foo8
Got: foo9
Got: foo7
Got: foo4
Got: foo2
queue length: 0

我建议在 monitor 模式下使用 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 只是删除 key (以确保已知状态); 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

关于c# - BookSleeve BlockingRemoveLeft 只返回一些插入列表的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21059099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com