我无法理解我的数据。在
我编写了一个测试,以确保我的Publisher类成功地发送数据,并且如果绑定到该类,则会收到该数据。在
类本身继承自Thread,并公开一个publish()方法,我可以调用该方法来通过Queue()将要广播的数据传递给订阅服务器。在
然而,在我的测试中,数据从未到达。我保证使用相同的
我认为这是个问题。在
我是一个ZeroMQ新手,但我以前已经设法让PubSub模式工作。在
测试代码:# Import Built-ins
import time
import json
import queue
from queue import Queue
from threading import Thread
# Import Third-Party
import zmq
def test_publisher_sends_data(self):
port = 667
name, topic, data = 'TestNode', 'testing', ['this', 'is', 'data']
encoded_name = json.dumps(name).encode('utf-8')
encoded_topic = json.dumps(topic).encode('utf-8')
encoded_data = json.dumps(data).encode('utf-8')
expected_result = (encoded_name, encoded_topic, encoded_data)
publisher = Publisher(port)
print("starting publisher")
publisher.start()
q = Queue()
def recv(q):
ctx = zmq.Context()
zmq_sock = ctx.socket(zmq.SUB)
print("Connecting to publisher")
zmq_sock.connect('tcp://127.0.0.1:%s' % port)
while True:
print("waiting for data..")
q.put(zmq_sock.recv_multipart())
print("data received!")
t = Thread(target=recv, args=(q,))
t.start()
print("sending data via publisher")
for i in range(5):
self.assertTrue(publisher.publish(name, topic, data))
time.sleep(0.1)
print("checking q for received data..")
try:
result = q.get(block=False)
except queue.Empty:
self.fail("Queue was empty, no data received!")
self.assertEqual(expected_result, result)
Publisher类
^{pr2}$