chaoshu的个人博客分享 http://blog.sciencenet.cn/u/chaoshu

博文

monitor.restart()错误

已有 1244 次阅读 2021-4-28 15:23 |个人分类:IoT|系统分类:科研笔记

image.png


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() --> 将线程停止 

image.png

但是在代码调式发现,通过self._is_running = False并没有把线程关闭,这个线程还在继续运行!

所以导致run函数里面的while self._is_running:不执行,频繁调用self._messenger.disconnect(),才导致错误发生!

image.png

image.png

解决办法:
彻底停止Module线程

查询Python停止线程的方法如下:

https://zhuanlan.zhihu.com/p/208260624

image.png


上述方法并不能解决问题:

真是的问题是,每一个Module的具体类对象都会创建一个线程,这些线程中存在MQTTMessenger的回调函数publish(),因此在stop()的时候,将messenger清空!self._messenger.__del__()

image.png

image.png




https://wap.sciencenet.cn/blog-858128-1284194.html

上一篇:树莓派更改Wi-Fi
下一篇:【课程推荐】快速获得cgcs2000和85高程
收藏 IP: 119.79.228.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 22:35

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部