“我正在python中使用Adafruit.io,我想在断开连接时收到通知吗?
我尝试使用多线程以便在断开连接时发出通知
def connected(client):
global loop_flag
loop_flag=0
# Connected function will be called when the client is connected to Adafruit IO.
# This is a good place to subscribe to feed changes. The client parameter
# passed to this function is the Adafruit IO MQTT client so you can make
# calls against it easily.
print('Connected to Adafruit IO! Listening for {0} changes...'.format(FEED_ID))
def checking():
global counter
while loop_flag==1:
print("Waiting for a call back{}".format(counter))
time.sleep(1)
counter=counter+1
if counter==5:
#client.disconnect()
print('disconnected from the service')
sys.exit(1)
t1=threading.Thread(target=connected(client))
t2=threading.Thread(target=checking)
t2.start()
t1.join()
t2.join()
client.loop_blocking()
如果没有连接esp,则表明没有连接,但是如果连接了一次,并且连接中断,则操作将永远循环,这是因为client.loop_blocking(),但是我无法对此发表评论标头解析数据