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

huangapple go评论92阅读模式
英文:

Compatibility of ZeroMQ sockets written on different languages

问题

我已经构建了一个使用Python编写的应用程序,基于**ZeroMQ**,但现在我面临性能问题。所以我决定重写我的应用程序的一些模块,使用另一种语言,比如说Golang。但是当我尝试在不同语言实现的套接字之间建立消息传递时,一切都不起作用。

到目前为止,我已经进行了搜索,但没有找到关于使用不同语言的ZeroMQ的兼容性问题的任何信息。

**所以问题是:**我可以使用Golang来实现基于ZeroMQ的服务器,并使用Python编写的客户端连接到它吗?或者我必须只使用一种语言?

**编辑:**这里是我正在尝试使其正常工作的典型服务器和客户端代码:

服务器:

import zmq

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

客户端:

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)
}

服务器在sock.recv()处停止响应。

英文:

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.<br><br>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?<br>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(&quot;tcp://*:57000&quot;)
msg = sock.recv()

client:

package main

import (
    zmq &quot;github.com/pebbe/zmq4&quot;
)

func main() {

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

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

Server stucks at sock.recv()

答案1

得分: 4

编程语言可以相互通信 - 是的,你可以用Go编写一个服务器,用Python编写一个客户端,并让它们相互通信。

如果你想使用原始套接字进行通信,请查看所需语言的文档,并确保序列化的数据在结构上匹配。

例如,你可以决定一个结构,并在Python中实现你的有效载荷,然后在Go中匹配该结构(在Go中可能有更好/标准库的方法;我对这种语言不是很熟悉)。在担心这些细节的维护和实现方面可能会带来麻烦,这就是为什么人们使用更高级的协议 - 这正是ZeroMQ的一个很好的用例。

如果你正在使用消息队列作为操作的基础,只需将其用作共享协议即可。如果你的Python代码可以与ZeroMQ通信,那么它就完成了它的工作 - 你的Go代码不需要知道它正在与Python通信。

在这种情况下,你的新Go服务器将绑定到ZeroMQ,你的Python客户端将绑定到ZeroMQ,而你的两种异构语言不需要彼此了解。

英文:

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.

答案2

得分: 1

这有点晚了,但我在寻找一个将Golang转换为Python的0mq实现时找到了这篇帖子。

在我的Golang代码中,我只是将zmq更改为**zmq4**,然后一切都正常工作了:

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)
}
英文:

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 &quot;github.com/pebbe/zmq4&quot;
)

func main() {

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

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

huangapple
  • 本文由 发表于 2016年3月10日 22:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/35918588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定