将ZMQ PUB与.connect()或.bind()方法一起使用有什么区别? [英] What is the difference between using ZMQ PUB with .connect() or .bind() methods?

查看:492
本文介绍了将ZMQ PUB与.connect()或.bind()方法一起使用有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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:


  1. 即假设有两个客户端( Machine1 Machine2 )和一个 Server

    每个客户端都必须使用 ZMQ ,并且 Server 必须从 Machine1 Machine2

  1. i.e. suppose that there are two clients (Machine1, Machine2) and a Server.
    Each client must publish data using ZMQ, 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() method doesn't support the localhost 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆