把队列传递出去了

时间:2017-10-06 20:59:19

标签: python queue

真的在这里摸不着头脑,试图为我创建的一个类编写一个环回测试并获得一些奇怪的行为。拥有队列的类似乎相当高兴并且可以将数据放入其队列中。但是调用类的函数似乎并不认为队列中有任何东西。没有其他人试图访问此队列,因为它是一个独立的单元测试

代码看起来像这样;

#mymqtt.py
import paho.mqtt.client as mqtt
import Queue

class mqttcomms:
    def __init__(self, address, topiclist, monitorlist):
    #   ....
        self.msgqueue = Queue.Queue()
    # ....
    def on_message(self, client, userdata, msg):
    #   .... something that creates;
        newmsg = ['something','in','here']

        self.msgqueue.put(newmsg)
        print 'Message put in queue: ' + str(not(self.msgqueue.empty()))


# tests.py
import mymqtt
# ....
minortest('Basic Loopback')
##############  Basic Loopback ##############
testMQserver = 'localhost'
topiclist = ['test']
monitorlist = ['var1', 'var2']
mqttComms = mymqtt.mqttcomms(testMQserver, topiclist, monitorlist)

message = 'Hello'
mqttComms.publishmessage(topiclist[0], monitorlist[0], message)
message_ok = False

time.sleep(5)

print 'Message in Queue: ' + str(not(mqttComms.msgqueue.empty()))

while not mqttComms.msgqueue.empty():
    print 'Something in Queue'
    msg = mqttComms.msgqueue.get()
    print msg

    checks = 0
    if msg[0] == topiclist[0]:
        checks += 1
    if msg[1] == monitorlist[0]:
        checks += 1
    if msg[2] == message:
        checks += 1

    if checks == 3:
        message_ok = True

我得到的是;

BASIC LOOPBACK 
MQTT: Connected to Server
MQTT: Published Hello to /test/var1
MQTT: Msg Received: test/var1 Hello
Message put in queue: True
Message in Queue: False

任何想法?如果有人能解释为什么会这样,我会非常感激,提前谢谢。

0 个答案:

没有答案