【问题标题】:unexpected behavior of push/pull sockets in netmq (zeromq)netmq (zeromq) 中推/拉套接字的意外行为
【发布时间】:2015-06-03 09:12:27
【问题描述】:

我正在尝试使用 netmq(zeroMQ 的端口)。这是我发现的问题。这是一个代码:

class Program
{
    private static NetMQContext context;

    static void Main(string[] args)
    {
        context = NetMQContext.Create();
        using (var puller = context.CreatePullSocket())
        {
            puller.Bind("tcp://127.0.0.1:5651");
            Thread.Sleep(500);
            for (int i = 0; i < 5; i++)
            {
                Task.Run(new Action(PusheThread));
            }
            Console.WriteLine("any key to start receive");
            Console.ReadKey();
            Msg msg = new Msg();
            msg.InitEmpty();
            for (; puller.TryReceive(ref msg, new TimeSpan(0, 0, 5)); msg = new Msg(), msg.InitEmpty())
            {
                var s = Encoding.UTF8.GetString(msg.Data);
                Console.WriteLine(s);
            }
        }
        Console.WriteLine("any");
        Console.ReadKey();
    }

    static void PusheThread()
    {
        var guid = Guid.NewGuid();
        Console.WriteLine("started: " + guid);
        using (var pusher = context.CreatePushSocket())
        {
            pusher.Connect("tcp://127.0.0.1:5651");
            for (int i = 0; i < 5; i++)
            {
                pusher.Send("helo! " + guid);
            }
        }
    }
}

如果我们运行此代码并在控制台中观察,我们会看到一些消息丢失了。 喜欢:

any key to start receive
started: 8aeca8e5-ed41-4055-ab72-750a0e61a680
started: 4211d77a-ad9f-40f1-9382-121156325128
started: bd735e75-2692-4abe-b8b1-fbddbe21e546
started: 6749d3bb-6b2b-4caa-b22e-755dba4d932d
started: 281ff59e-4430-4fc6-9435-4dc2c5e6015e
helo! 8aeca8e5-ed41-4055-ab72-750a0e61a680
helo! 6749d3bb-6b2b-4caa-b22e-755dba4d932d
helo! 8aeca8e5-ed41-4055-ab72-750a0e61a680
helo! 6749d3bb-6b2b-4caa-b22e-755dba4d932d
helo! 8aeca8e5-ed41-4055-ab72-750a0e61a680
helo! 6749d3bb-6b2b-4caa-b22e-755dba4d932d
helo! 8aeca8e5-ed41-4055-ab72-750a0e61a680
helo! 6749d3bb-6b2b-4caa-b22e-755dba4d932d
helo! 8aeca8e5-ed41-4055-ab72-750a0e61a680
helo! 6749d3bb-6b2b-4caa-b22e-755dba4d932d
any

正如我们所见,4211d77a-ad9f-40f1-9382-121156325128bd735e75-2692-4abe-b8b1-fbddbe21e546 和其他一些地方没有消息。 是多线程的问题吗?还是我做错了什么?谢谢。

【问题讨论】:

  • 检查推送线程中连接和发送的错误,看看是否有任何弹出。
  • 我发现这个问题只存在于netmq。如果我使用本机 zeromq .net 包装器一切正常

标签: sockets zeromq netmq


【解决方案1】:

问题出在PusheThread 中,您只需快速杀死新创建的PushSocket

static void PusheThread()
{
    var guid = Guid.NewGuid();
    Console.WriteLine("started: " + guid);
    using (var pusher = context.CreatePushSocket())
    {
        pusher.Connect("tcp://127.0.0.1:5651");
        for (int i = 0; i < 5; i++)
        {
            pusher.Send("helo! " + guid);
        }
        Thread.Sleep(5000);
    }
}

尽管这不是一个可靠的解决方案,但它应该证明问题在于PushSocket 在能够发送消息之前被using 语句处理。

有些消息甚至只是显示了 NetMQ 和 ZeroMQ 有多快:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2022-01-02
    • 2019-08-29
    • 2023-03-31
    相关资源
    最近更新 更多