如何在两个 VM 之间使用 pyZMQ 发布/订阅

编程入门 行业动态 更新时间:2024-10-10 23:18:43
本文介绍了如何在两个 VM 之间使用 pyZMQ 发布/订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个 VM(VirtualBOx、Ubuntu 18.04 和 python-zmq)在同一台物理机 (Win10) 中运行.两台机器都配置为Bridge,可以ping通192.168.1.56-192.168.1.65.我遵循了本教程

解决方案

问:您是否尝试过这个,或者您知道可能是什么原因?"

是的,复制粘贴源代码(使用与您相同的 Scenario #2,有一个单独的 SUB ) 工作正常,而是使用下面修改后的代码模板.

A PUB-archetype Publisher :

使用 ZeroMQ v.2.11,py-2.7:

导入zmq随机导入导入系统导入时间上下文 = zmq.Context()尝试:socket = context.socket( zmq.PUB )socket.bind( "tcp://*:%s" % "5556" if len( sys.argv ) <2 else int( sys.argv[1] ) )socket.setsockopt( zmq.LINGER, 0 )为真:主题 = random.randrange( 9999, 10005 )messagedata = random.randrange(1, 215) - 80打印 "Topis = {0: >6d}: DATA = {1: >4d}".format( topic, messagedata )socket.send( "%d %d" % ( topic, messagedata ) )时间.sleep( 1 )除了:打印EXC'd here"最后:将优雅地关闭 Socket()-instance(s) 和 TERM Context()-instance(s)"socket.close()上下文.term()

A SUB-原型监听器:

导入系统进口zmq上下文 = zmq.Context()尝试:socket = context.socket( zmq.SUB )打印将开始连接:用于进一步从天气服务器收集更新...";socket.connect( "tcp://localhost:%s" % "5556" if len( sys.argv ) 2:socket.connect( "tcp://localhost:%s" % int( sys.argv[2] ) )socket.setsockopt( zmq.LINGER, 0 )# 订阅邮编,默认为NYC, 10001经过;topicfilter = "10001";socket.setsockopt( zmq.SUBSCRIBE, topicfilter )# 处理 5 个更新总价值 = 0对于范围 (5) 中的 update_nbr:字符串 = socket.recv()主题,消息数据 = string.split()total_value += int( messagedata )打印主题,消息数据打印更新:主题 '%s' 的平均 messagedata 值为 %dF";%(主题过滤器,total_value/update_nbr)除了:打印EXC'd here"最后:打印将优雅地关闭 Socket()-instance(s) 和 TERM Context()-instance(s)";socket.close()上下文.term()

您的分布式计算修改 SUB 端代码以定义无效的 TCP/IP 目标后,实验失败,其中代码由于显而易见的原因无法成功.connect().在此处阅读有关解决方案的信息.

I have two VMs (VirtualBOx, Ubuntu 18.04 and python-zmq) running within the same physical machine (Win10). Both machines are configured as Bridge and they can be ping successfully 192.168.1.56-192.168.1.65. I've followed this tutorial learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/pubsub.html, however, it doesn't work. On one machine "server", the data is printed, however, at the subscriber "client", it doesn't receive anything.

Have you tried this or do you know which could be the cause?

解决方案

Q : "Have you tried this or do you know which could be the cause?"

Yes, a copy-paste source code ( used the same Scenario #2, as you did, having a solo SUB ) works fine, yet rather use the modified code template below.


A PUB-archetype Publisher :

Using ZeroMQ v.2.11, py-2.7:

import zmq import random import sys import time context = zmq.Context() try: socket = context.socket( zmq.PUB ) socket.bind( "tcp://*:%s" % "5556" if len( sys.argv ) < 2 else int( sys.argv[1] ) ) socket.setsockopt( zmq.LINGER, 0 ) while True: topic = random.randrange( 9999, 10005 ) messagedata = random.randrange( 1, 215) - 80 print "Topis = {0: >6d}: DATA = {1: >4d}".format( topic, messagedata ) socket.send( "%d %d" % ( topic, messagedata ) ) time.sleep( 1 ) except: print "EXC'd here" finally: "WILL gracefully CLOSE Socket()-instance(s) and TERM Context()-instance(s)" socket.close() context.term()


A SUB-archetype Listener :

import sys import zmq context = zmq.Context() try: socket = context.socket( zmq.SUB ) print "WILL start connecting: for further collecting updates from weather server..." socket.connect( "tcp://localhost:%s" % "5556" if len( sys.argv ) < 2 else int( sys.argv[1] ) ) if len( sys.argv ) > 2: socket.connect( "tcp://localhost:%s" % int( sys.argv[2] ) ) socket.setsockopt( zmq.LINGER, 0 ) # Subscribe to zipcode, default is NYC, 10001 pass; 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 "UPDATE: Average messagedata value for topic '%s' was %dF" % ( topicfilter, total_value / update_nbr ) except: print "EXC'd here" finally: print "WILL gracefully CLOSE Socket()-instance(s) and TERM Context()-instance(s)" socket.close() context.term()


Your distributed-computing experiment fails after modifying the SUB-side code on defining an invalid TCP/IP-target where the code cannot, for obvious reasons, successfully .connect(). Go get a read about the solution here.

更多推荐

如何在两个 VM 之间使用 pyZMQ 发布/订阅

本文发布于:2023-11-22 01:18:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615411.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   如何在   pyZMQ   VM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!