||
mqtt - Unexpected disconnection from 127.0.0.1:1883
mqtt - Reconnecting to 127.0.0.1:1883 ...
mqtt - Connected "scheduler1" to 127.0.0.1:1883
mqtt - Unexpected disconnection from 127.0.0.1:1883
mqtt - Reconnecting to 127.0.0.1:1883 ...
mqtt - Unexpected disconnection from 127.0.0.1:1883
mqtt - Connected "fileExporter" to 127.0.0.1:1883
mqtt - Reconnecting to 127.0.0.1:1883 ...
mqtt - Unexpected disconnection from 127.0.0.1:1883
mqtt - Connected "com1" to 127.0.0.1:1883
mqtt - Reconnecting to 127.0.0.1:1883 ...
mqtt - Connected "preProcessor" to 127.0.0.1:1883
mqtt - Unexpected disconnection from 127.0.0.1:1883
mqtt - Connected "scheduler1" to 127.0.0.1:1883
mqtt - Unexpected disconnection from 127.0.0.1:1883
mqtt - Reconnecting to 127.0.0.1:1883 ...
mqtt - Reconnecting to 127.0.0.1:1883 ...
mqtt - Unexpected disconnection from 127.0.0.1:1883
mqtt - Connected "cloudExporter" to 127.0.0.1:1883
mqtt - Reconnecting to 127.0.0.1:1883 ...
MQTT出现Connected和Unexpected disconnection交替往复!
从截图的代码查看:
self._is_running = False
才会调用
self._messenger.disconnect()
分析代码发现:
Module(threading.Thread)是一个线程类,包含两个功能:
1)MQTT:MQTTMessenger
2)Worker:具体的类对象,比如 com1-->SerialPort
在kill或者remove程序的时候,操作如下:
self._modules[name].stop_worker() --> 将模块(类对象)is_running = False
self._modules[name].stop() --> 将线程停止
但是在代码调式发现,通过self._is_running = False并没有把线程关闭,这个线程还在继续运行!
所以导致run函数里面的while self._is_running:不执行,频繁调用self._messenger.disconnect(),才导致错误发生!
解决办法:
彻底停止Module线程
查询Python停止线程的方法如下:
https://zhuanlan.zhihu.com/p/208260624
上述方法并不能解决问题:
真是的问题是,每一个Module的具体类对象都会创建一个线程,这些线程中存在MQTTMessenger的回调函数publish(),因此在stop()的时候,将messenger清空!self._messenger.__del__()
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2025-1-15 21:07
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社