doufusi2013
doufusi2013
2012-10-10 13:32
浏览 68
已采纳

如何在不同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
    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.

    点赞 评论

相关推荐