如何配置多个 PUB/单个 SUB python ZMQ Ubuntu [英] How configure a Multiple PUB/ single SUB python ZMQ Ubuntu

查看:146
本文介绍了如何配置多个 PUB/单个 SUB python ZMQ Ubuntu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 VM(VirtualBOx、Ubuntu 18.04 和 python-zmq[16.0.2-2build2])在同一台物理机 (Win10) 中运行.两台机器都配置为Bridge,可以ping通192.168.1.66-192.168.1.55.我遵循了本教程 https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/pubsub.html.如果 PUB(服务器)配置为

I have two VMs (VirtualBOx, Ubuntu 18.04 and python-zmq[16.0.2-2build2]) running within the same physical machine (Win10). Both machines are configured as Bridge and they can be ping successfully 192.168.1.66-192.168.1.55. I've followed this tutorial https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/pubsub.html. It works if PUB (server) is configured as

import zmq
import random
import sys
import time

port = "5557"
if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % port)
while True:
    topic = random.randrange(9999,10005)
    messagedata = random.randrange(1,215) - 80
    print "%d %d" % (topic, messagedata)
    socket.send("%d %d" % (topic, messagedata))
    time.sleep(1)

和 SUB(客户)为

import sys
import zmq

port = "5557"
if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)
    
if len(sys.argv) > 2:
    port1 =  sys.argv[2]
    int(port1)

# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)

print "Collecting updates from weather server..."
socket.connect ("tcp://192.168.1.66:%s" % port)

if len(sys.argv) > 2:
    socket.connect ("tcp://localhost:%s" % port1)
# Subscribe to zipcode, default is NYC, 10001
topicfilter = "10001"
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)

# Process 5 updates
total_value = 0
for update_nbr in range (5):
    string = socket.recv()
    topic, messagedata = string.split()
    total_value += int(messagedata)
    print topic, messagedata

print "Average messagedata value for topic '%s' was %dF" % (topicfilter, total_value / update_nbr)

由于我想要一个带有多个服务器 (PUB) 的单个客户端 (SUB),它们可以是数百甚至数千个,因此为每个 PUB 配置一个 IP 是不可行的.有没有不指定IP的订阅方式?或者至少是一个广播.我尝试在 socket.connect ("tcp://IP:%s" % port):

Since I want a single client (SUB) with multiple servers (PUB) where they can be hundreds even thousands, it is unfeasible to configure a single IP for each PUB. Is there a way to subscribe without specifying the IP? Or at least a broadcast one. I've tried to configure at client in socket.connect ("tcp://IP:%s" % port):

*"

给出错误:

Traceback (most recent call last):
  File "sub_client.py", line 18, in <module>
    socket.connect ("tcp://*:%s" % port)
  File "zmq/backend/cython/socket.pyx", line 528, in zmq.backend.cython.socket.Socket.connect (zmq/backend/cython/socket.c:5980)
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:8400)
zmq.error.ZMQError: Invalid argument

192.168.1.1 (GW), 192.168.1.255 (broadcast), localhost/127.0.0.1 和它的 IP (192.168.1.55) ->收不到消息

192.168.1.1 (GW), 192.168.1.255 (broadcast), localhost/127.0.0.1 and it's IP (192.168.1.55) -> does not receive messages

192.168.1.66(服务器的 IP)->可以接收消息但在大规模系统中不实用

192.168.1.66 (server's IP) -> Does receive messages but not practical in a large scale system

有什么办法可以解决这个问题吗?

Any way to solve this?

推荐答案

问题:有什么办法可以解决这个问题吗?

避免违背任何 API 记录的属性.虽然 .bind()-method 可以用于 tcp://-transport-class 尝试确实绑定到任何 localhost 端的 IP 地址,.connect() 方法,显然不能.

Avoid going against any API-documented property. While a .bind()-method can for the tcp://-transport-class try to indeed bind to any localhost-side IP-addresses, the .connect()-method, for obvious reasons can't.

正如在 ZMQError 中通知的那样:

As was notified in the ZMQError:

socket.connect (tcp://*:%s"% 端口)
zmq.error.ZMQError:无效参数

更正IP地址目标,
.connect( "tcp://{0:}:{1:}".format( IP, PORT ) )-method 尝试ring-so-as-to-get-connection".

Correct the IP-address target, where shall the
.connect( "tcp://{0:}:{1:}".format( IP, PORT ) )-method try to "ring-so-as-to-get-connection".

这篇关于如何配置多个 PUB/单个 SUB python ZMQ Ubuntu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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