将ZMQ PUB与.connect()或.bind()方法一起使用有什么区别? [英] What is the difference between using ZMQ PUB with .connect() or .bind() methods?
问题描述
在Python ZMQ 发布者/订阅者示例模板中,发布者使用 .bind()
方法,而订阅者使用 .connect()
方法连接到绑定IP地址。
In Python ZMQ publisher/subscriber sample template, the publisher uses .bind()
method and the subscriber uses .connect()
method, that connected to the bind IP address.
但是我们可以替换 .bind()
和 .connect()
彼此。
But we can replace .bind()
and .connect()
each with the other.
我的问题是,下面确定的两个案例之间有什么区别?
(在这些案例中,两个脚本可以正常工作)
My question is that what is the difference between two cases that determined below?
(two scripts in these cases work fine)
第一种情况,默认为:
pub1.py:
import zmq
import time
from datetime import datetime
def create_pub_socket():
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://127.0.0.1:9002") # notice
return socket
def publish(pub_socket):
message = {
'data': 'hi my name is benyamin',
'time': datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
}
pub_socket.send_json(message, 0)
return message
if __name__ == '__main__':
socket = create_pub_socket()
while True:
print('\n')
print('publisher: ', publish(socket))
time.sleep(1)
sub1.py:
import zmq
if __name__ == '__main__':
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, "")
socket.connect("tcp://127.0.0.1:9002") # notice
while True:
data = socket.recv_json()
print('subscriber: ', data)
print('\n')
第二种情况是修改后的设置,它颠倒了 .connect()
和 .bind()
方法的使用:
And the second case, as the modified setup, that reversed the use of the .connect()
and .bind()
methods:
pub2.py:
import zmq
import time
from datetime import datetime
def create_pub_socket():
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.connect("tcp://127.0.0.1:9002") # notice
return socket
def publish(pub_socket):
message = {
'data': 'hi my name is benyamin',
'time': datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
}
pub_socket.send_json(message, 0)
return message
if __name__ == '__main__':
socket = create_pub_socket()
while True:
print('\n')
print('publisher: ', publish(socket))
time.sleep(1)
sub2.py:
import zmq
if __name__ == '__main__':
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, "")
socket.bind("tcp://127.0.0.1:9002") # notice
while True:
data = socket.recv_json()
print('second subscriber: ', data)
print('\n')
推荐答案
此处(单个发布者和订阅者)没有区别,但在其他情况下(多个发布者或订阅者)没有区别,具体取决于您的政策:
There is no difference at here (single publisher and subscriber) but in other scenarios (multi publishers or subscribers), there is a difference depending on your policy:
-
即假设有两个客户端( Machine1 , Machine2 )和一个 Server 。
每个客户端都必须使用ZMQ
,并且 Server 必须从 Machine1 和 Machine2 :
i.e. suppose that there are two clients (Machine1, Machine2) and a Server.
Each client must publish data usingZMQ
, and the Server must subscribe that data from Machine1 and Machine2:
-
Machine1 ->具有发布者 (使用
.connect(服务器IP)
)
Machine2 - >具有发布者(具有 .connect(服务器IP)
)
服务器->具有订户(具有 .bind(服务器IP /自IP)
)
如您所提到的那样,我们在问题中使用第二种情况。
As you can see in the mentioned scenario we use the second case in the question.
如果有两个订阅者和一个发布者,则必须放置 .bind()
方法在发布者中,将 .connect()
方法放置在订阅者中(问题的第一种情况)。
Whereas, if we have two subscribers and a publisher, we must place the .bind()
method in the publisher and place .connect()
method in the subscribers (first case in the question).
[注意]:
-
.bind()
方法不支持localhost
作为一个IP,而.connect()
方法可以连接到.bind()
方法中定义的每个IP地址:为什么zeromq在本地主机上不起作用?
.bind()
method doesn't support thelocalhost
as an IP, while.connect()
method could be connected to each IP address that defined in.bind()
method: Why doesn't zeromq work on localhost?
这里是更改 .bind()
和的另一个示例。 connect()
位置:此链接
Here is another example of changing .bind()
and .connect()
locate: This-Link
这篇关于将ZMQ PUB与.connect()或.bind()方法一起使用有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!