我正在使用 NSQ 消费者从 NSQ 队列中获取消息,并通过 getData 函数添加到 BLEmanagment 类中的 JSON 变量中。但是当我尝试在下面运行代码时,处理程序类会永远运行并阻止主线程。blemanagement.getData() 从未调用过。
import threading
import gnsq
import json
class BLEmanagment():
def __init__(self):
self.JSON = None
def startNOTIFY(self):
print("startNOTIFY")
nsqconsume.start_consumer()
def stopNOTIFY(self):
print("stopNOTIFY")
nsqconsume.stop_consumer()
def getData(self):
print("getData")
self.JSON = nsqconsume.get_json()
class NSQConsumer():
def __init__(self):
self.JSON = None
self.consumer = None
def stop_consumer(self):
print("stop_consumer")
self.consumer.close()
def get_json(self):
print("get_json")
return self.JSON
def start_consumer(self):
self.consumer = gnsq.Consumer('sensors', 'python_consumer', 'localhost:4150')
print("start_consumer")
@self.consumer.on_message.connect
def handler(consumer, message):
value_string = message.body.decode('utf-8')
self.JSON = json.loads(value_string)
print(value_string)
self.consumer.start()
nsqconsume = NSQConsumer()
blemanagement = BLEmanagment()
t1 = threading.Thread(blemanagement.startNOTIFY())
t1.run()
blemanagement.getData()
我的问题是 Handler(blemanagement.startNOTIFY()) 函数如何在后台踏板中永久运行,而 BLEmanagement 类的其他功能也可以从主踏板访问。