ZeroMQ知道请求来自哪个Go协程。

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

ZeroMQ knows which go routine a request came from

问题

我正在尝试弄清楚如果我有两个例程调用相同的zeromq套接字,当例程1的socket.recv()返回时,套接字会知道返回给例程1(例程2也是如此,以此类推)。

因此...使用具有请求/响应语义的消息总线,在Go语言中最好的模式是什么?

如果有帮助的话...考虑简单的海盗模式,并在该模式下使用zeromq实现RPC样式的调用集。

Socket; //zeromq套接字
//例程1
socket.send(data) //我需要在这里为这个例程添加一些标识符吗?
socket.recv() //等待直到我收到那个发送的响应
//例程2
socket.send(data)
socket.recv()

所以在这种情况下,我不知道例程1或例程2的响应会先返回。我如何确保当我收到socket的响应时...我可以通知正确的例程的recv()函数。

谢谢!

英文:

I am trying to figure out if i have two routines calling the same zeromq socket if when the return from socket.recv() on routine1 returns.. the socket will know to return to routine1 (ditto for routine2, ... routinen).

Hence... using a messaging bus with request/response semantics, what is the best pattern to approach this in go?

If it helps.. think about the simple pirate pattern and implementing an RPC style call set in that pattern with zeromq.

Socket; //zeromq socket
//routine 1
socket.send(data) // do i need some identifier here for this routine?
socket.recv() // wait until i get a response from that send
//routine 2 
socket.send(data)
socket.recv()

so in this case i have no idea if the response from routine1 or routine2 will come back first. How can i make sure that when i get a response to socket... i can notify the correct routines' recv() function.

Thanks!

答案1

得分: 2

我不相信一个套接字可以在使用zeromq的情况下被多个线程共享并发地使用。

可能你需要创建一些类似goroutine的东西来监听请求,发送/接收zmq,然后回复。

例如:(未经测试的代码)

type Req struct {
   Data []byte 
   Reply chan []byte
}

go func() { // 可能不是zmq的API,但你应该能理解这里的思路
    for req := <- requests {
         socket.send(req.Data)
         req.Reply <- socket.recv() 
    }
}
英文:

I don't believe a socket can be shared, concurrently, between threads using zeromq

Likely you need to do some kind of goroutine that listens for requests, sends / receives zmq, and then replies.

for example: (untested code)

type Req struct {
   Data []byte 
   Reply chan []byte
}

go func() { // probably not the zmq api, but you should get the idea here
    for req := &lt;- requests {
         socket.send(req.Data)
         req.Reply &lt;- socket.recv() 
    }
}

huangapple
  • 本文由 发表于 2015年5月28日 05:30:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/30493492.html
匿名

发表评论

匿名网友

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

确定