doufusi2013 2012-10-10 13:32 采纳率: 0%
浏览 76
已采纳

如何在不同Goroutine中的ZeroMQ上下文之间进行通信?

I'm using this as boilerplate, except that in the same program I also have some goroutines that are the workers and connect to the backend endpoint, tcp://127.0.0.1:5560.

What I would like to do is have it connect through a more efficient way, such as ipc://, inproc://, or even unix sockets. I've tried those, and it didn't work. Channels is a no-go with ZeroMQ right ?

So how do I connect different goroutines with ZeroMQ contexts, without tcp ? Is there a better alternative ?

update: The code:

// Simple message queuing broker
// Same as request-reply broker but using QUEUE device
//
// Author:  Brendan Mc.
// Requires: http://github.com/alecthomas/gozmq

package main

import (
    zmq "github.com/alecthomas/gozmq"
)

func startWorker() {
    context, _ := zmq.NewContext()
    defer context.Close()

    worker, _ := context.NewSocket(zmq.REP)
    //err := worker.Connect("ipc:///backend")  // Tried it, but nothing
    //err := worker.Connect("inproc:///backend")  // Tried it, but nothing
    err := worker.Connect("tcp://127.0.0.1:5560") // this works
    if err != nil {
        fmt.Println(err)
    }

    for {
        data, err := worker.Recv(0)
        fmt.Println(string(data))
        worker.Send([]byte("I got your data"), 0)
    }
}

func main() {
    context, _ := zmq.NewContext()
    defer context.Close()

    // Socket facing clients
    frontend, _ := context.NewSocket(zmq.ROUTER)
    defer frontend.Close()
    frontend.Bind("tcp://*:5559")

    // Socket facing services
    backend, _ := context.NewSocket(zmq.DEALER)
    defer backend.Close()
    //backend.Bind("ipc:///backend")  // Tried it, but nothing
    //backend.Bind("inproc:///backend")  // Tried it, but nothing
    backend.Bind("tcp://*:5560") // this works

    for i := 0; i < 4; i++ {
        go startWorker() // Start workers in a separate goroutine
    }

    // Start built-in device
    zmq.Device(zmq.QUEUE, frontend, backend)

    // We never get here…
}
  • 写回答

1条回答 默认 最新

  • dongliehuan3925 2012-10-11 17:34
    关注

    In order to use the inproc:// transport, all of the sockets need to be sharing the same Context(which is thread-safe).

    Also if you're using the same Context, you do not need any backend I/O threads for ZMQ

    You do not mention which OS you're running under, but the ipc:// transport is only available under most *nix. Under windows you're only able to have the following transports: tcp://, inproc://, pgm://. Check out the zmq_connect documentation for more information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog