dta25920
dta25920
2016-03-10 14:00
浏览 107

用不同语言编写的ZeroMQ套接字的兼容性

I've built an app written on python, based on ZeroMQ, but now I'm facing perfomance issues. So I decided to rewrite some modules of my app using, let's say, Golang. But when I try to establish messaging between sockets, implemented by different languages, anything does not work.

I've searched so far, but I didn't find any info about compatibility problems of ZeroMQ using different languages.

So the question is: Can I use golang for server implementation based on ZeroMQ and client written on python to connect to it?
Or do I have to use only one language?

EDIT: here are typical server and client that I'm trying to get working properly

server:

import zmqctx = zmq.Context()
sock = ctx.socket(zmq.REP)
sock.bind("tcp://*:57000")
msg = sock.recv()

client:

package main

import (
    zmq "github.com/pebbe/zmq4"
)

func main() {

    ctx, _ := zmq.NewContext()
    sock, _ := ctx.NewSocket(zmq.REQ)

    sock.Connect("tcp://localhost:57000")
    sock.Send("simple message", 0)
}

Server stucks at sock.recv()

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doupao6011
    doupao6011 2016-03-10 14:12
    已采纳

    Programming languages are able to communicate with each other -- yes, you can write a server in Go and a client in Python and have them communicate with each other.

    If you're trying to communicate using raw sockets, look at the documentation for your desired languages and make sure that the serialized data match in structure.

    e.g. You could decide on a struct and implement your payload in Python or code and then match that structure in Go (there may be a better/stdlib way in Go; I'm not very experienced in this language). The potential maintenance and implementation headache in worrying about these details is why people use higher-level protocols -- exactly a good use case for ZeroMQ.

    If you're using a message queue as the backbone of your operation, simply use that as the shared protocol. If your Python code can speak with ZeroMQ, then it's doing its job correctly -- there's no need for your Go code to know it's speaking to Python.

    In this case, your new Go server would bind to ZeroMQ, your Python client would bind to ZeroMQ, and your two heterogenous languages need to know nothing about each other.

    点赞 评论
  • dongying6659
    dongying6659 2016-12-31 13:56

    This is a little bit late but I found this post while I am trying to find a golang-to-python 0mq implementation.

    In my golang code, I have just changed zmq to zmq4 and everything worked for me:

    import (
        zmq4 "github.com/pebbe/zmq4"
    )
    
    func main() {
    
        ctx, _ := zmq4.NewContext()
        sock, _ := ctx.NewSocket(zmq4.REQ)
    
        sock.Connect("tcp://localhost:57000")
        sock.Send("simple message", 0)
    }
    
    点赞 评论

相关推荐